开发者

Besides the main artifact jar, generate another jar with some dependencies, not all, for distribution outside Maven

I understand that users using Maven should not care about dependencies as Maven will download them as specified in the pom. However I want to distribute another jar to non-maven users. So I still want to generate the raw jar for Maven, no problem there. But I would also want to have the ability to generate another jar 开发者_高级运维with some dependencies included within. By some dependencies I mean that I will choose some of the dependencies as defined in the pom.xml, and include them in this other jar I will distribute myself.

So let's say I have 10 dependencies in my pom.xml. dep1.jar, dep2.jar, ... , dep10.jar.

The main jar generated for distribution with MAVEN will not have any of these dependencies, of course. But I would like the ability to generate another jar somehow with dep3.jar and dep4.jar included.

Any help will be highly appreciated!

EDIT: Directing me to a maven plugin does not help. But giving me a pom.xml example does. :)


Take a look at the maven-assembly-plugin. You can configure it to create a JAR containing all of the dependencies of your project, along with the project's source code. You can then bind an execution of the plugin to the package phase so this mega-jar is built as a part of the regular lifecycle.


Not sure what you mean by jars included into generated jars, but you have several options to play with.

First of all you can use Maven dependency plugin to fetch dependencies and then generate a bundle using Maven assembly plugin.

Alternatively, you can use Maven Shade plugin or Maven JarJar plugin to merge multiple jars into one.


@matt b has answered this, but since you wanted code snippets here it goes. This is one way to do this.

Create a file, say assemblyDescriptor.xml in src/main/assembly folder as follows:

<assembly>
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/my-distribution</outputDirectory>
            <includes>
                <include>group3:dep3</include>
                <include>group6:dep6</include>
                ...
            </includes>
            <unpack>true</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

Specify the following plugin in your pom, if required within a <profile> section for conditional invocation.

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/assemblyDescriptor.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>

run mvn package or mvn install and look for a zip file in addition to the project jar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜