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

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.

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.

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.

Related

How to stop maven from output "[WARNING] Configuration option 'appendAssemblyId' is set to false."?

I have a maven project generating a jar and not appending maven-assembly-plugin's appendAssemblyId.
How can I get maven to stop issuing the warning: "Configuration option 'appendAssemblyId' is set to false." ?
EDIT: I was asked to include the pom file, which I'm not permitted to do. I'm including the maven-assembly-plugin block which causes the error.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>some.company.hello</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- prevent appending <descriptorRef> "jar-with-dependencies" to final jar name -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<!-- For inheritance merges -->
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This answer explains the technical details, but I think the key is the phrase that you mentioned in your question.
I have a maven project generating a jar and not appending maven-assembly-plugin's appendAssemblyId.
If I'm understanding correctly, I take this to mean that in your pom file, you have an entry that says
<packaging>jar</packaging>
What this does is create a jar file that competes with the output of the maven-assembly-plugin, and maven-assembly-plugin wins out, resulting in only one jar file. In fact, if you were to set appendAssemblyId back to true, you will get two files in the output directory - <artifact>.jar and <artifact>-jar-with-dependencies.jar.
So, first, you should remove the <packaging> tag I mentioned above, and add the following to the <executions> section of your pom file so that there won't be two conflicting/competing jar files (and the warning will go away):
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>

How to combine javafx-maven-plugin and maven-assembly-plugin?

I have a JavaFx project which I want to reduce to one jar file.
My pom contains a java-fx plugin:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.4</version>
<configuration>
<mainClass>application.Main</mainClass>
<jfxAppOutputDir>${project.build.directory}/jfx/app</jfxAppOutputDir>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
and an assembly plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
When I call "mvn package" a single jar file is being build but it does not contain the jfx-jar file but contains some sort of "jar wihout jfx support"-version.
How can I tell the Maven Assembly Plugin to use the jar version from the jfx plugin (which is located in jfxAppOutputDir (../jfx/app/))?
Found the problem myself.
the jar file with the "no main manifest attribute"-error was created by maven-jar-plugin. But it is possible to configure it to omit that creation. If you are interested please take a look:"what-is-the-best-way-to-avoid-maven-jar"
the assembly plugin worked correctly. It was my fault. I thought that the assembly plugin creates a jar file with all dependencies including all resource files(all files from the resource folder). My mistake. "jar-with-dependencies" includes all jars belonging to the project, nothing more.

How do I create an uber source jar with Maven?

Is there a well-known way to create an uber source jar? In other words, a jar of all the source code for a project and all its dependencies (or at least those that have a -sources.jar)?
I've looked into doing it with the maven-assembly-plugin, but using a dependencySet with includes of *.*.*.sources.* (or *.sources) doesn't work because those are not actually dependencies of the project, and I don't want to add them all.
You can use the maven-shade-plugin to create an uber jar. Just include the following within your <build> tag -
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>source-jar</id>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createSourcesJar>true</createSourcesJar>
<artifactSet>
<includes>
<include>...</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
To modify the configuration, you can use Resource Transformers within org.apache.maven.plugins.shade.resource package.
And to define the contents of the jar, you can further use includes and excludes within the filters.
I found some information on working with sources in the maven-dependency-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>src-dependencies</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/sources</outputDirectory>
<includeGroupIds>{your group prefix}</includeGroupIds>
<includes>**/*.java</includes>
<includeScope>runtime</includeScope>
</configuration>
</execution>
So if I do that, and then run a maven-assembly-plugin referencing the unpacked files, I can do it in two steps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<execution>
<id>uber-source</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>ubersource.xml</descriptor>
</descriptors>
<outputDirectory>${deploy.internal.directory}</outputDirectory>
<finalName>${project.artifactId}</finalName>
</configuration>
</execution>
</executions>
</plugin>
with a file set in the assembly descriptor ubsersource.xml:
<fileSet>
<directory>${project.build.directory}/sources</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
And then I get my uber source jar...
There is perhaps a subtle distinction in the way the maven-assembly-plugin and maven-dependency-plugin treats sources. If you reference classifier sources in a dependencySet of an assembly descriptor, it looks for sources that are actual dependencies in your pom -- not that useful. However, in maven-dependency-plugin, referencing sources classifier means that sources of your dependencies. Hence why this solution works.
I also wrapped this up in my own plugin using mojo-executor to make it single step, and single declaration in my pom, but that's optional
This is a lot more pom code, but I like it better than the maven-shade-plugin because it does just what I want, and nothing more.

Build JAR with custom manifest in maven

I am trying to get maven to create the artifact jar with a custom MANIFEST.MF.
Sounds like an easy task using the following snippet:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.directory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
Problem is, that the maven-jar-plugin still "messes" with the manifest I created manually. The documentation states
The content of your own manifest file will be merged with the entries
created by Maven Archiver
Unfortunately this is not the case for the Manifest-Version. It will always be set to "1.0". (I know that in theory this is correct, but for reasons I cannot influence, I need a different value in there).
Any ideas on how to step the jar plugin from touching my manifest at all or at least keeping its hands of the Manifest-Version?
I finally managed to solve my problem using the maven-antrun-plugin:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<jar file="${project.build.directory}/${project.build.finalName}.jar" update="true" manifest="${project.build.directory}/META-INF/MANIFEST.MF" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

include package in maven while preparing jar

I have below project structure. I wanted to prepare a jar with dependencies including different packages which has .java files.
Project Structure:
src/com/rev/automation/utilities
src/com/rev/automation/testdata
src/com/rev/automation/pages
Main Class:
org.openqa.selenium.remote
How to include "src/com/rev/automation" packages into the jar in maven? i'm preparing the jar using below code, But it is not including packages and files present in "src/com/rev/automation". Kindly suggest
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>org.openqa.selenium.remote.RemoteWElement</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
While using Maven, you need to follow the Maven Standard Directory Structure.
In your case, create a folder structure like src/main/java and put all your code in java directory. So it should now look like src/main/java/com/rev/automation etc.
If you follow this structure, then your package will get included in the jar.
Update:
If you absolutely don't want to / cannot do what is mentioned above, alternatively, you can use the Maven Build Helper plugin. This will tell maven to include the specified directory as an additional source directory.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src</source> // specify your directory here
...
</sources>
</configuration>
</execution>
</executions>
</plugin>

Resources