Why does the Maven goal “package” include the resources in the jar, but the goal “jar:jar” doesn't?

与世无争的帅哥 提交于 2019-12-11 09:48:49

问题


when I package my project with the Maven goal "package", the resources are included as well. They are originally located in the directory "src/main/resources". Because I want to create an executable jar and add the classpath to the manifest, I'm using maven-jar-plugin.

I've configured it as the following likes:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>at.program.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Why won't the jar file created with "jar:jar" include my resources as well. As far as I'm concerned it should use the same directories as the "package" goal (which are in my case inherited from the Maven Super POM).


回答1:


The jar:jar goal of the Maven JAR plugin is used to "build a JAR from the current project" and does only one thing: it packages the content of target/classes into a JAR in the target directory, and that's all. So, when you run mvn jar:jar, the plugin configuration in your pom is used, but jar:jar won't do more things than what I mentioned. If target/classes is empty or doesn't exist, no classes or resources will be packaged in the resulting JAR.

The package phase is a build lifecycle phase and when you invoke mvn package, all phases before package will be executed (process-resources, compile, process-test-resources, etc) and will trigger the plugin goals bound to these phases. So, for a project with a <packaging> of type jar, jar:jar is bound to package and will be run during the package phase but prior to that, the goals bound to phases preceding package will be triggered, including the one that copies the resources into target/classes.




回答2:


You can try using maven.jar.includes in you jar plugin configuration.




回答3:


As I've found out, the maven-jar-plugin binds itself automatically to the package goal when the project packaging is set to "jar". So the "package" goal does everything that I want, including appending the classpath to the manifest and setting a main class.



来源:https://stackoverflow.com/questions/2787114/why-does-the-maven-goal-package-include-the-resources-in-the-jar-but-the-goal

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