Configure javafx-maven-plugin to create jar file without version number - java

I cannot stop my javafx-maven-plugin to insert the version number into the jars file name. I tried to set finalName to "${pom.artifactId}" but that doesn't help.
What can I do?
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.4</version>
<configuration>
<mainClass>application.Main</mainClass>
<jfxAppOutputDir>${project.build.directory}</jfxAppOutputDir>
<finalName>${pom.artifactId}</finalName>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>

It is an old question, but:
1.Insert final jar Name in this way:
<build>
<finalName>your.customname.without.versionNumber</finalName>
2.Edit javafx-maven-plugin configuration, add this inside the configuration tag:
<jfxMainAppJarName>${project.build.finalName}.jar</jfxMainAppJarName>
The complete configuration should looks like this:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<jfxMainAppJarName>${project.build.finalName}.jar</jfxMainAppJarName>
<mainClass>com.my.amazingapp.MainClassName</mainClass>
<vendor>The Tiger Programmer</vendor>
<manifestAttributes>
<developer>Me</developer>
<contact>me#company.com</contact>
</manifestAttributes>
</configuration>
</plugin>
Now if your finalName is "MyAmazingProject", after you run mvn clean jfx:jar you will find a file MyAmazingProject.jar under
/target/jfx/app/MyAmazingProject.jar
This jar is a executable jar with Manifest Info.

Related

Maven artifact plugin buildinfo remove unwanted buildinfo information

On using
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-artifact-plugin</artifactId>
<version>3.3.0</version>
</plugin>
I am able to get buildinfo file generated as following as per https://reproducible-builds.org/docs/jvm/
#### Work In Progress ####
buildinfo.version=1.0-SNAPSHOT
name=name
group-id=groupId
artifact-id=artifact id
version=version
**source information**
no scm configured in pom.xml
**build instructions**
build-tool=mvn
**effective build environment information**
java.version=11
java.vendor=Oracle corporation
os.name=Linux
**Maven rebuild instructions and effective environment:**
mvn.version=Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
**output**
outputs.0.filename=pom file name
outputs.0.length=<1234
outputs.0.checksums.sha512=abcd
Apart from name,group-id,artifact-id and version, i don't want other properties to be generated in that file. how can i configure that using maven-artifact-plugin
I have checked the doc https://maven.apache.org/plugins/maven-artifact-plugin/plugin-info.html, couldn't find example of removing unwanted information from getting generated in that file.
You may achieve something similar using maven-resources-plugin and optionally copy-rename-maven-plugin (if you need to get particular name of buildinfo file)
contents of .buildinfo:
name=${project.name}
group-id=${project.groupId}
artifact-id=${project.artifactId}
version=${project.version}
maven configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}</directory>
<filtering>true</filtering>
<includes>
<include>.buildinfo</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-and-rename-file</id>
<phase>process-resources</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/.buildinfo</sourceFile>
<destinationFile>${project.build.directory}/${project.artifactId}-${project.version}.buildinfo</destinationFile>
</configuration>
</execution>
</executions>
</plugin>

How to compile a Maven Java with the librarys? [duplicate]

I have a code base which I want to distribute as jar. It also have dependency on external jars, which I want to bundle in the final jar.
I heard that this can be done using maven-assembly-plug-in, but I don't understand how. Could someone point me to some examples.
Right now, I'm using fat jar to bundle the final jar. I want to achieve the same thing using maven.
Note: If you are a spring-boot application, read the end of answer
Add following plugin to your pom.xml
The latest version can be found at
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>CHOOSE LATEST VERSION HERE</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
After configuring this plug-in, running mvn package will produce two jars: one containing just the project classes, and a second fat jar with all dependencies with the suffix "-jar-with-dependencies".
if you want correct classpath setup at runtime then also add following plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
For spring boot application use just following plugin (choose appropriate version of it)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
You can use the maven-shade-plugin.
After configuring the shade plugin in your build the command mvn package will create one single jar with all dependencies merged into it.
Maybe you want maven-shade-plugin, bundle dependencies, minimize unused code and hide external dependencies to avoid conflicts.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>true</createDependencyReducedPom>
<dependencyReducedPomLocation>
${java.io.tmpdir}/dependency-reduced-pom.xml
</dependencyReducedPomLocation>
<relocations>
<relocation>
<pattern>com.acme.coyote</pattern>
<shadedPattern>hidden.coyote</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
References:
http://maven.apache.org/plugins/maven-shade-plugin/plugin-info.html
http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html
actually, adding the
<archive>
<manifest>
<addClasspath>true</addClasspath>
<packageName>com.some.pkg</packageName>
<mainClass>com.MainClass</mainClass>
</manifest>
</archive>
declaration to maven-jar-plugin does not add the main class entry to the manifest file for me.
I had to add it to the maven-assembly-plugin in order to get that in the manifest
You can use the onejar-maven-plugin for packaging. Basically, it assembles your project and its dependencies in as one jar, including not just your project jar file, but also all external dependencies as a "jar of jars", e.g.
<build>
<plugins>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note 1: Configuration options is available at the project home page.
Note 2: For one reason or the other, the onejar-maven-plugin project is not published at Maven Central. However jolira.com tracks the original project and publishes it to with the groupId com.jolira.
An alternative is to use the maven shade plugin to build an uber-jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version> Your Version Here </version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
Read if you want to use the maven-assembly-plugin.
As other answers have already outlined, it seems that the maven-shade-plugin offers more features and is the recommended plugin to build a fat jar, but in case you would like to use the maven-assembly-plugin the following plugin configuration will work.
The answer of #jmj explains that the correct classpath can be setup with an additional maven-jar-plugin, but this will only add the classpath to the original jar and not the fat jar. The information must instead be directly included into the configuration section of the maven-assembly-plugin.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.package.YourMainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When you now run maven package, your normal and fat jar will be created and you can run your fat jar with java -jar yourJar.jar.

Getting maven to execute a program and store the output into a property that can be used in a .jar manifest

I would like to execute git describe as part of a maven build and use the resulting output in the manifest for building a .jar package.
I know how to do this in ant via the <exec> task with outputproperty to an ant property variable, but I have very little experience with Maven and don't even know where to look.
Is this possible?
I found this in a sample pom.xml file so adding something to the manifest looks pretty easy:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>my.class.here.Myclass</mainClass>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Not sure how to capture command execution though.
Here is a suggested approach:
Use the Exec Maven Plugin for launching your git commands and write to a properties file (name=value pattern, if possible)
Use the Properties Maven Plugin to load the configuration
The configuration loaded can then be used as properties in your POM. We are basically dynamically creating properties of our build. To do so (to use these properties), the steps above must be executed as early as possible in the build flow (i.e. validate or initialize phase).
Below an example of flow, just tested and work perfectly (on Windows machine):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>generation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>retrieve-config</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>echo</executable>
<arguments>
<argument>jar.name=from-exec</argument>
<argument>></argument>
<argument>config.properties</argument>
</arguments>
<workingDirectory>${basedir}/src/main/resources/</workingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>read-properties</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/config.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${jar.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Basically, the exec plugin attached to the validate phase will be executed at the beginning of the build, writing to a config.properties file (via the echo command) the content jar.name=from-exec.
Then the properties plugin attached to the initialize phase will read that config.properties file and load the properties to be used as part of the build.
Then, as an example, the jar plugin will use that property as part of its configuration (the <finalName>${jar.name}</finalName> part).
Running mvn clean package, you will find the from-exec.jar file in the target folder.
If you can't get a way of having the result of git describe as name=value pattern, you can (worst case) have two Exec Maven Plugin executions, the first writing to the file the property name and the equals character (i.e. via an echo), the second (git describe) appending to the file the property value.
There is a Maven plugin here https://github.com/ktoso/maven-git-commit-id-plugin that will do what you want.
If you hook into your build it will generate a Maven variable named ${git.commit.id.describe} that you can then use Maven's resource filtering to dynamically modify your manifest.
Building on what #A_Di-Matteo said, you can get the git tag into the properties file like this
In the maven exec plugin.
<execution>
<id>set-git-tag</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>echo git.tag=`git describe --always --dirty=-modified`</argument>
<argument>></argument>
<argument>config.properties</argument>
</arguments>
<workingDirectory>${basedir}/src/main/resources/</workingDirectory>
</configuration>
</execution>
You can also append to an existing file like so
<execution>
<id>set-git-tag</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>echo git.tag=`git describe --always --dirty=-modified`</argument>
<argument>>></argument>
<argument>${basedir}/src/main/resources/config.properties</argument>
</arguments>
</configuration>
</execution>
The limitation here is that it requires bash.

Exclude META-INF/maven folder from the generated jar file

I am trying to create a jar file which has all the necessary classes extracted within the jar. But for few dependent jar like log4j, it creates some folders inside META-INF/maven/*. I have a limitation that the server in which I will be placing the generated jar file will not have Internet connectivity. So if there is any content in this META-INF/maven/* folder then it gives me an error.
My maven descriptor looks like the following
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<minimizeJar>true</minimizeJar>
<finalName>myclient</finalName>
</configuration>
</plugin>
</plugins>
</build>
I am able to extract the required class files in the generated jar but the maven folder is still getting generated under META-INF. I have to manually delete the folder to make everything work. Please advice on how to automate this removal of maven folder from the generated jar file.
You can use filters inside the maven-shade-plugin configuration to exclude everything that is under META-INF/maven for every artifact:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
A solution for the maven-jar-plugin can be found here.
Simple add this to either it is a jar,war,ear plugin
<configuration>
....
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
....
</configuration>

Multiple Source Folders for Eclipse Java Project & Maven

Question/Problem:
How to add an additional source folder to a standard java console Maven project using Eclipse (Luna) so that Maven sees the path for jar build.
The expected result is to somehow configure pom.xml so that Maven plugins in Eclipse can be executed cleanly.
Assumptions - a successful add of an additional source folder via project (right click) -> new -> source folder.
To let Maven know about the new source folder for building a jar I had to add the following to my pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>[your source folder goes here]/**/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>[your source folder goes here]</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Be sure to add the pluginManagement tags around plugins as omitting this tag prevented the mojo plugin to recognize the executions tag.
Perhaps more later on the success of the actual jar construction...
Add generated sources into configuration of maven-compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<generatedSourcesDirectory>[additional directory]</generatedSourcesDirectory>
</configuration>
</plugin>
or provide additional execution:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-additional-sources</id>
<goals><goal>compile</goal></goals>
<configuration>
<source>[additional sources]</source>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources