Unable to disable generation of empty JAR (maven-jar-plugin)

你离开我真会死。 提交于 2019-12-11 02:56:32

问题


Sometimes, my Talend Open Studio components have resources but not Java sources (they are purely metadata components). I need to disable the generation of JAR files in such a case.

I configured the maven-jar-plugin this way:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <forceCreation>false</forceCreation>
            <skipIfEmpty>true</skipIfEmpty>
            <useDefaultManifestFile>false</useDefaultManifestFile>
        </configuration>
      </plugin>

but I still get the ${project.name}.jar file with pom.properties, pom.cml, the manifest and an empty file App.class containing only "class {}"

While I can disable the includes of all maven stuff using this:

<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>

I still get a JAR with the manifest file inside it

Are there some configuration parameters I misconfigured?


回答1:


Most efficient way to disable the creation of jars is to configure the maven-jar-plugin like this:

<plugins>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
            <execution>
                <id>default-jar</id>
                <phase>none</phase>
            </execution>
        </executions>
    </plugin>
</plugins>

It will place the default jar creation in the none phase, it will never be run.




回答2:


I found the solution by myself, even if it's only a workaround. I delete the JAR using a delete antrun task if /src/main/java directory doesn't exist:

<!-- remove the empty JAR if not needed -->
<if>
    <not><available file="${basedir}/src/main/java" type="dir" /></not>
    <then>
    <delete file="${project.build.directory}/${project.name}-${project.version}.jar"/>
    </then>
</if>

this task requires antcontrib to work properly and, ofc, it doesn't work if you plan to do releases with maven (but it's ok for metadata-only components, like Talend Open Studio plugins)




回答3:


You can instruct maven-jar-plugin to not generate META-INF/maven/*/pom. files, as explained in Maven Archiver Reference.

Also, you can use its skipIfEmpty option.

Following code combines both these (just to have them copy-paste ready):

...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <skipIfEmpty>true</skipIfEmpty>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                </archive>
...

This works fine, but when you do mvn install, it fails due to missing project artifact. Similar problem will probably be with mvn deploy and with release, but I didn't check these.

However, if you can live with antrun's delete, the property skipIfEmpty will probably work well for you, and is a bit more elegant. At least it does not introduce a new execution and its dependencies etc.



来源:https://stackoverflow.com/questions/13218313/unable-to-disable-generation-of-empty-jar-maven-jar-plugin

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