4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Gabriele B
  • 2,665
  • 1
  • 25
  • 40
  • 1
    Change the packaging type into something different than jar but based on the snippet of your pom i'm not sure if this would work well. – khmarbaise Nov 04 '12 at 12:19
  • I think it's not a good idea, since for those components that have a src dir, a standard JAR file must be packaged actually. – Gabriele B Nov 04 '12 at 12:26

3 Answers3

10

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.

maba
  • 47,113
  • 10
  • 108
  • 118
  • It's surely the most elegant way to do so. But I need to disable the jar creation conditionally (ie. only if /src/main/java doesn't exist) – Gabriele B Nov 07 '12 at 09:46
  • If you do "mvn install" this will fail with an error "Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.5.2:install (default-install) on project xxx: The packaging for this project did not assign a file to the build artifact" – tok Feb 14 '19 at 07:42
  • This fixed it for me: ` maven-jar-plugin default-jar none maven-install-plugin default-install none ` – tok Feb 14 '19 at 07:53
1

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.

Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
  • 1
    tnx Petr for your answer, but i noticed that with your config (it's the one I used, actually) the JAR file is still generated, with the manifest file only – Gabriele B Nov 07 '12 at 09:42
  • strange - didn't you forget to apply the `skipIfEmpty` property ? because in my testing pom this really causes `mvn clean package` to stop creating it. Or you forgot the `clean` ? _(No offense - it just works, reproducibly, on my machine, so I can't believe :) )_ – Petr Kozelka Nov 07 '12 at 12:16
  • 100% sure :) maybe it's because i have a (although empty) resource dir? – Gabriele B Nov 07 '12 at 16:22
0

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)

Gabriele B
  • 2,665
  • 1
  • 25
  • 40