Multiple assemblies from one maven project [duplic

2019-01-18 18:15发布

问题:

This question already has an answer here:

  • Creating Two Executable Jars Using maven-assembly-plugin 1 answer

we have different java source code "projects". 3 projects are completly identical(fatclient, same dependencies etc.) - there is only another main class that have to be invoked.

Today we have one base-project with a main class:

<project>
    <groupId>net.company.BaseTool</groupId>
    <artifactId>BaseTool</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>BaseTool</name>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>net.company.BaseTool</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

and other projects that depending on the base-project

<project>
    <groupId>net.company.AnotherTool</groupId>
    <artifactId>AnotherTool</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AnotherTool</name>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>net.company.AnotherTool</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>net.company.BaseTool</groupId>
            <artifactId>BaseTool</artifactId>
            <version>1.1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

We are doing this, because we need simple double-click startable applications.

But I dont want to create an extra java project for each application.

My question is: Is it possible to create multiple assemblies from one project? And if yes, how it should be done.

Here comes the solution

<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>my.company.toolbox</groupId>
    <artifactId>toolbox</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>toolbox</name>
    <url>http://wiki.company.my/toolbox</url>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <finalName>toolbox</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>ToolOne</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>ToolOne</finalName>
                            <archive>
                                <manifest>
                                    <mainClass>my.company.ToolOne</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                    <execution>
                        <id>ToolTwo</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>ToolTwo</finalName>
                            <archive>
                                <manifest>
                                    <mainClass>my.company.ToolTwo</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                    <execution>
                        <id>ToolThree</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>ToolThree</finalName>
                            <archive>
                                <manifest>
                                    <mainClass>my.company.ToolThree</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                    <execution>
                        <id>ToolFour</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>ToolFour</finalName>
                            <archive>
                                <manifest>
                                    <mainClass>my.company.ToolFour</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

回答1:

You can use different executions of the assembly plugin and then attach the resulting assembly to the build. Of course you have to configure different final names for the artifacts in the assembly plugin configuration.