'How can I build a separate jar file with Maven - isolated from the default jar?

Forgive me, I don't quite know how to ask this question in a single sentence, because I don't know how to define what I'm looking for other than to tell you what I'm doing now, and what I would like to do...

In my POM file, I have this plugin setup

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <skipIfEmpty>true</skipIfEmpty>
        <archive>
            <manifestEntries>
                <Automatic-Module-Name>com.simtechdata.sceneonefx</Automatic-Module-Name>
                <Program-Version>${version}</Program-Version>
                <Implementation-Version>${version}</Implementation-Version>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

And the way I normally use maven with this project, is I deploy it to the repository by typing: mvn clean deploy

What I would LIKE to do, is engage maven with a different command-line option, then have Maven build the jar file, but give it a statically assigned name and drop it into a statically assigned path (not target). And I'd like to create that other jar file by doing something like this:

mvn otherJar

or whatever ...

Is this possible?

Here is the complete pom file if that helps:

<?xml version="1.0" encoding="UTF-8"?>
<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.simtechdata</groupId>
    <artifactId>SceneOneFX</artifactId>
    <version>1.0.16</version>
    <packaging>jar</packaging>

    <name>${project.artifactId}</name>
    <description>A Utility Library that makes creating and managing JavaFX Scenes easy.</description>
    <url>https://github.com/EasyG0ing1/SceneOneFX</url>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>Mike Sims</name>
            <email>[email protected]</email>
            <organization>Simtech Data Services, Inc.</organization>
            <url>https://github.com/EasyG0ing1</url>
            <roles>
                <role>developer</role>
                <role>architect</role>
                <role>technical</role>
                <role>complaint department</role>
            </roles>
        </developer>
    </developers>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <scm>
        <connection>scm:git:git://github.com/EasyG0ing1/SceneOneFX.git</connection>
        <developerConnection>scm:git:[email protected]:EasyG0ing1/SceneOneFX.git</developerConnection>
        <url>https://github.com/EasyG0ing1/SceneOneFX</url>
        <tag>HEAD</tag>
    </scm>

    <profiles>
        <!-- GPG signature on release -->
        <profile>
            <id>release-sign-artifacts</id>
            <activation>
                <property>
                    <name>performRelease</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <build>
        <plugins>
            <!-- Nexus staging Maven plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>toolchain</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <toolchains>
                        <jdk>
                            <version>1.8</version>
                            <vendor>sun</vendor>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.13</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
            <!-- Maven jar plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <skipIfEmpty>true</skipIfEmpty>
                    <archive>
                        <manifestEntries>
                            <Automatic-Module-Name>com.simtechdata.sceneonefx</Automatic-Module-Name>
                            <Program-Version>${version}</Program-Version>
                            <Implementation-Version>${version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!-- Maven Deploy plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>3.0.0-M2</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven release plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <localCheckout>true</localCheckout>
                    <pushChanges>false</pushChanges>
                    <mavenExecutorId>forked-path</mavenExecutorId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.scm</groupId>
                        <artifactId>maven-scm-provider-gitexe</artifactId>
                        <version>2.0.0-M1</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!-- Maven source plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifestEntries>
                                    <Automatic-Module-Name>com.simtechdata.sceneonefx</Automatic-Module-Name>
                                    <Program-Version>${version}</Program-Version>
                                    <Implementation-Version>${version}</Implementation-Version>
                                </manifestEntries>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven javadoc plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven GPG plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>sign-artifact</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Maven Compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


Solution 1:[1]

Below is minimal example for maven-dependency-plugin configuration:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resulting-jar</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <phase>none</phase>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>demo</artifactId>
                                    <version>${project.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>/Users/apanfilov</outputDirectory>
                                    <destFileName>demo.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

output:

mvn clean package dependency:copy@copy-resulting-jar
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.example:demo:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 13, column 12
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 17, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo ---
[INFO] Deleting /Users/apanfilov/work/demo/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/apanfilov/work/demo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo ---
[INFO] Building jar: /Users/apanfilov/work/demo/target/demo-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy (copy-resulting-jar) @ demo ---
[INFO] Configured Artifact: com.example:demo:0.0.1-SNAPSHOT:jar
[INFO] Copying demo-0.0.1-SNAPSHOT.jar to /Users/apanfilov/demo.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.933 s
[INFO] Finished at: 2022-05-09T15:50:16+10:00
[INFO] ------------------------------------------------------------------------

Alternatively to avoid mentioning plugin in cmd it is possible to assign "correct" phase for execution, conditionally enable it via profile, etc.

Solution 2:[2]

This is how I ended up doing it ... in order for the deploy plugins to remain functioning, I ended up added multiple executions to the jar plugin like this

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<executions>
    <execution>
        <goals>
            <goal>jar</goal>
        </goals>
        <id>local</id>
        <phase>package</phase>
        <configuration>
            <skipIfEmpty>true</skipIfEmpty>
            <archive>
                <manifestEntries>
                    <Automatic-Module-Name>com.simtechdata.sceneonefx</Automatic-Module-Name>
                    <Program-Version>${version}</Program-Version>
                    <Implementation-Version>${version}</Implementation-Version>
                </manifestEntries>
            </archive>
            <outputDirectory>/Users/michael/Java/SceneOneFX</outputDirectory>
            <classifier>local</classifier>
            <finalName>SceneOneFX</finalName>
        </configuration>
    </execution>
    <execution>
        <id>deploy</id>
        <configuration>
            <skipIfEmpty>true</skipIfEmpty>
            <archive>
                <manifestEntries>
                    <Automatic-Module-Name>com.simtechdata.sceneonefx</Automatic-Module-Name>
                    <Program-Version>${version}</Program-Version>
                    <Implementation-Version>${version}</Implementation-Version>
                </manifestEntries>
            </archive>
        </configuration>
    </execution>
</executions>
</plugin>

Then multiple executions for the compiler as well:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
    <execution>
        <goals>
            <goal>compile</goal>
        </goals>
        <id>local</id>
        <phase>package</phase>
        <configuration>
            <outputFileName>SceneOneFX</outputFileName>
            <includes>
                <include>**/*.java</include>
            </includes>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </execution>
    <execution>
        <id>deploy</id>
        <configuration>
            <includes>
                <include>**/*.java</include>
            </includes>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </execution>
</executions>
</plugin>

Then, when I want the local jar, I do this:

mvn clean package

And when I need to deploy, I do this:

mvn clean deploy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrey B. Panfilov
Solution 2 Michael Sims