32

I am using maven assembly plugin to generate a .tar file contain several other files, dependent jars. All the files are being copied correctly to the given folders in the config assembly.xml.

I would like to include the original project jar too in the final tar file, but not seeing it currently in it.

I do get the following message when I issue assembly:single goal:

[WARNING] Cannot include project artifact: com.my.newpkg.project1:jar:0.0.3.0; it doesn't have an associated file or directory.

After reading over SO, it seems adding the following configs to pom.xml should add the jar, but still not getting the expected result.

config in pom.xml

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.4</version>
   <configuration>
       <archive>
           <manifest>
               <mainClass>com.my.newpkg.project1.MainClass</mainClass>
               <addClasspath>true</addClasspath>
               <classpathPrefix>lib/</classpathPrefix>
           </manifest>
       </archive>
   </configuration>
</plugin>

snippet of assembly.xml

<formats>
    <format>dir</format>
    <format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <excludes>
            <exclude>junit:junit</exclude>
        </excludes>
        <fileMode>0755</fileMode>
    </dependencySet>
</dependencySets>

So, the configs are not yet correct? or Am I missing something here?

How can we add the current project jar into the final tar

mtk
  • 13,221
  • 16
  • 72
  • 112

1 Answers1

63

In <dependencySet> you can exclude the current project jar by saying <useProjectArtifact>false</useProjectArtifact>, but it's true by default, so it should work.

From the warning, it seems that you need to do mvn package first, but due to some internal maven issue, it does not work if you do mvn package and mvn assembly:single in separate commands.

It works if you do mvn package assembly:single in one command.

Alternatively, add maven-assembly-plugin in your pom and bind it to the 'package' phase so it will trigger automatically on mvn package:

   <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>tar-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>etc/assembly.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • 12
    Executing `mvn package assembly:single` is including the jar, but if we run them as separate commands, then the same warning is shown, and the jar doesn't get included. **Sidenote:** the jar is available in target folder before running `assembly:single`. – mtk Jan 11 '13 at 12:25
  • 2
    If that's indeed the case, you might have discovered a new bug. It looks like exactly the opposite of http://jira.codehaus.org/browse/MASSEMBLY-372. Can you report a new bug there, and attach your full project? If the assembly is an essential part of your build, it's best practice to include it in your pom, anyway. If you don't want it every time, you can use a to separate it. – GeertPt Jan 11 '13 at 14:39
  • @mtk/@greyfairer, I think this entry https://issues.apache.org/jira/browse/MASSEMBLY-94 is about the warning you see when running both commands separately. It seems to be a fundamental and unfixable issue with maven, though. – Siggen Sep 03 '15 at 11:13
  • The binding of the plugin to the 'package' phase is recommended by its [documentation](https://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly). – otterrisk Nov 02 '18 at 11:20
  • Thanks @otterisk. I had this problem as well and had nothing to do with not having run package. I added the execution of it to the package phase as described in the documentation you link to and it worked. Cheers! – crowmagnumb May 22 '20 at 19:57