简体   繁体   中英

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

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).

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 .

您可以尝试在jar插件配置中使用maven.jar.includes

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM