How to package a jar and all dependencies within a

2019-07-19 17:47发布

问题:

I have a maven project with like ten dependencies. Before, I used to pack all of that in a single jar thanks to maven-assembly-plugin:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>create-executable-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>myApp.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

But now, I added a step before. I have a plugin that will generate the jar of my application. So I just want the assembly plugin to add the dependencies to this jar. Unfortunately, the plugin doesn't use this jar, but instead, seems tu be using the result from the compiler.

is there a way to specify that I want the plugin to use the previously generated jar instead of the result from the compiler ?

回答1:

Try using the maven-shade-plugin. You need something like:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>