Johannes Ernst
Johannes Ernst

Reputation: 3196

Change jar filename for output of maven jar-with-dependencies?

I create a JAR containing the code from several projects with the maven-assembly-plugin's jar-with-dependencies descriptorRef as described here: Including dependencies in a jar with Maven.

But how do I get it to produce output file foo-1.0.jar instead of the ugly foo-1.0-SNAPSHOT-jar-with-dependencies.jar? I don't need the default JAR that doesn't contain other projects.

Upvotes: 1

Views: 2021

Answers (2)

Johannes Ernst
Johannes Ernst

Reputation: 3196

Here's what I ended up doing. In my pom.xml:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.3</version>
    <executions>
        <execution>
            <id>all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptors>
            <descriptor>all.xml</descriptor>
        </descriptors> 
    </configuration>
</plugin>

I specify a home-grown assembly descriptor, which I get by copying the assembly file jar-with-dependencies.xml from https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html into local file all.xml, changing id jar-with-dependencies to all. Voilà, now the generated filename is foo-1.0-SNAPSHOT-all.jar, which works for my purposes.

Upvotes: 1

Fabien
Fabien

Reputation: 879

In the maven-assembly-plugin you can add an optional parameter called appendAssemblyId (which is set to true by default) in the configuration tag of your assembly execution.
The use of this tag will generate two warnings indicating that you might override the main build of the artifact (done by the maven-jar-plugin).
If you don't want to override this jar with appendAssemblyId set to false, you can decide to build your assembly in another folder with the property outputDirectory.
Or the other solution if you are ok with the fact to have append something at the end of the name of your jar is to create your own assembly descriptor.

(for more information about the existing parameters or how to create your own assembly descriptor you can see the plugin documentation here : https://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html)

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.5.3</version>
  <executions>
    <execution>
      <id>jar-with-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <appendAssemblyId>false</appendAssemblyId>
        <outputDirectory>${project.build.directory}/my-assembly/</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Edit : I edited my answer in order to make it more complete.

Upvotes: 3

Related Questions