Maven build assembly with dependencies

前端 未结 2 702
醉梦人生
醉梦人生 2020-12-03 11:21

I have standalone java application, which i wanted to package as: myapp.jar. And all dependent jars to be copied to \'alternate folder\'. Ideally, i would like to have maven

相关标签:
2条回答
  • 2020-12-03 11:51

    You can try as

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.core.App</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/bin.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
    

    src/assembly/bin.xml

    <assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
        <formats>
            <format>dir</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>/</outputDirectory>
                <unpack>false</unpack>
                <includes>
                    <include>${artifact}</include>
                </includes>
            </dependencySet>
            <dependencySet>
                <outputDirectory>/lib</outputDirectory>
                <unpack>false</unpack>
                <excludes>
                    <exclude>${artifact}</exclude>
                </excludes>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    Run it as

    mvn clean package assembly:single
    

    Details: http://maven.apache.org/plugins/maven-assembly-plugin/index.html

    0 讨论(0)
  • 2020-12-03 11:57

    Please follow the below steps:

    Step 1: You should have your regular Jar present in Maven Package inside folder - target. If not, Run as > Maven Install(at this time Goals in Maven Build Run configuration will be 'clean package')

    Step 2: Add below script in pom.xml

        <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Full path with Main class(App) for example com.first.apicode.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
      </build>
    

    Step 3: Change Goals in Maven Build Run configuration to 'clean package assembly:single' and Click Run

    THAT'S ALL!

    Now you will be able to see runnable JAR with all dependencies inside target folder like YourArtifactID-0.0.1-SNAPSHOT-jar-with-dependencies.jar

    0 讨论(0)
提交回复
热议问题