maven assembly include the current project jar in the final zip/tar

断了今生、忘了曾经 提交于 2019-11-29 10:50:22

问题


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


回答1:


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, I guess you forgot to do mvn package first, so the jar is not available from the target directory.

Or 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>


来源:https://stackoverflow.com/questions/14276453/maven-assembly-include-the-current-project-jar-in-the-final-zip-tar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!