开发者

Recommended practices for distributing/using maven projects outside the build lifecycle

开发者 https://www.devze.com 2023-03-31 16:10 出处:网络
There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.

There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.

Things I have tried

1) Maven-shade plugin For some use cases ( such as hadoop jobs ) I find it convenient to use the Maven shade plugin to integrate with the package step ( it builds an all inclusiv开发者_JAVA百科e uber-jar ). The downside is that the uber-jar is too massive. Also, I cant get the maven shade to work on datanucleus dependencies as it messes up something.

2) distribute the dependencies along with the jar.

//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass

generates a directory with all the dependencies along with the jar.

What I would like to do is

3) just be able to distribute the jar and handle the dependencies while executing the jar. Since mvn package puts the pom in the jars manifest folder, all the information is there right ? . Now, I would like it if there was a one line command to be able to run this jar asking maven to manage the dependencies. Even more awesome if someone knows if such a thing can be used as a hadoop job.


I think there is no solution for question in your point 3.

My three options, in all cases you can run application with simple java -jar target/project.jar. Nothing more is needed.

1. Maven Assembly Plugin with jar-with-dependencies descriptor

(manifest in jar configuration is missed here)

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.2</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

2. Maven dependency plugin

Maven dependency plugin, properly jar configured (classpath) and maven assembly custom descriptor for packing all together.

(you can simplify this, if your project is not using snapshot dependencies)

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>your.package.MainApp</mainClass>
                <packageName>your.package</packageName>
                <classpathLayoutType>custom</classpathLayoutType>
                <customClasspathLayout>lib/$${artifact.artifactId}.$${artifact.extension}/customClasspathLayout>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>true</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>test</excludeScope>
                <includeScope>compile</includeScope>
                <stripVersion>true</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

3. Maven One Jar

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <execution>
            <configuration>
                <attachToBuild>true</attachToBuild>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
0

精彩评论

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