开发者

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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜