java 如何使用 Maven 创建具有完整依赖项的独立应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10224631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create a standalone application with dependencies intact using Maven?
提问by Paul Taylor
I have a desktop Java application built using Maven 2 (but I could upgrade to Maven 3 if that helps) with a number of open source dependencies. I'm now trying to package it up as a standalone to make it available to end users without them needing to install maven or anything else.
我有一个使用 Maven 2 构建的桌面 Java 应用程序(但如果有帮助,我可以升级到 Maven 3)和许多开源依赖项。我现在正在尝试将其打包为独立的,以便最终用户可以使用它,而无需他们安装 maven 或其他任何东西。
I've successfully used maven-assembly-plugin to build a single jar containing all dependencies but this is not really what I want because when using LGPL libraries you are meant to redistribute the libraries you are using as separate jars.
我已经成功地使用 maven-assembly-plugin 构建了一个包含所有依赖项的 jar 但这并不是我真正想要的,因为在使用 LGPL 库时,您打算重新分发您正在使用的库作为单独的 jar。
I want Maven to build a zip containing a jar with my code and a MANIFEST.MF
that refers to the other jars I need together with the other jars. This seems like standard practice but I cannot see how to do it.
我希望 Maven 构建一个 zip,其中包含一个带有我的代码的 jar 以及一个MANIFEST.MF
引用我需要的其他 jar 和其他 jar 的 jar。这似乎是标准做法,但我不知道该怎么做。
Here's an extract from my pom
这是我的 pom 的摘录
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.company.widget.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.company.widget.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
EDIT:Taken on Kals idea
编辑:采用 Kals 的想法
created a file called descriptor.xml
创建了一个名为 descriptor.xml 的文件
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
and pom contains:
和 pom 包含:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.company.widget.cmdline.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now maintains the jar and puts them all in the lib folder, including my own code
现在维护jar并将它们全部放在lib文件夹中,包括我自己的代码
采纳答案by Kalpak Gadre
Try creating a custom assembly descriptor and add a dependencySet and make sure you specify, unpack as false.
尝试创建一个自定义程序集描述符并添加一个依赖项集,并确保您指定,解包为 false。
Use this as assembly descriptor,
使用它作为程序集描述符,
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
Store this file to say src/main/assembly/assembly.xml and update your assembly plugin configuration in pom.xml like this.
将此文件存储为 src/main/assembly/assembly.xml 并像这样在 pom.xml 中更新您的程序集插件配置。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
</configuration>
</execution>
</executions>
</plugin>
Here is assembly descriptor reference if you need anything more
如果您需要更多,这里是程序集描述符参考
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
回答by palto
You should check out the Maven Appassembler plugin. You can get a lot more robust package using it than rolling your own assembly.
您应该查看Maven Appassembler 插件。使用它可以获得比滚动自己的程序集更强大的软件包。
It generates helpful startup scripts for Unix and Windows that allow you to set predefined JAVA VM options, commandline parameters and classpath.
它为 Unix 和 Windows 生成有用的启动脚本,允许您设置预定义的 JAVA VM 选项、命令行参数和类路径。
It also has a concept of configuration directory where you can copy default configurations that user can later change. You can also set the configuration directory to be available on the classpath.
它还具有配置目录的概念,您可以在其中复制用户可以稍后更改的默认配置。您还可以将配置目录设置为在类路径上可用。
Dependencies can be saved in a Maven style "repo" or you can use a flat style "lib" directory.
依赖项可以保存在 Maven 样式的“repo”中,或者您可以使用平面样式的“lib”目录。
You still need the assembly plugin for creating a zip or tar archive.
您仍然需要程序集插件来创建 zip 或 tar 存档。
Here's an example appassembler configuration:
这是一个示例 appassembler 配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<programs>
<program>
<mainClass>com.mytools.ReportTool</mainClass>
<name>ReportTool</name>
</program>
</programs>
<assembleDirectory>${project.build.directory}/ReportTool</assembleDirectory>
<repositoryName>lib</repositoryName>
<repositoryLayout>flat</repositoryLayout>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
To get a zip archive I use the this assembly:
要获取 zip 存档,我使用以下程序集:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/ReportTool</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
回答by magomi
As far as I know it is possible to redistribute also LGPL libraries into your own packages as long as they are untouched. The maven assembly plugin creates a jar that contains the original jars inside a lib folder of the archive. So far you are fulfilling the LGPL.
据我所知,只要未受影响,也可以将 LGPL 库重新分发到您自己的包中。Maven 程序集插件会创建一个 jar,其中包含存档的 lib 文件夹中的原始 jar。到目前为止,您正在履行 LGPL。
Maybe this questiongives some more information about this topic.
也许this question提供了有关此主题的更多信息。
(Disclaimer: I'm not a lawyer, so please crosscheck this information ;) )
(免责声明:我不是律师,所以请交叉核对这些信息;))
回答by PShetty
Add the following plugins in pom.xml. Check the value at mainClass,classpathPrefix,addClasspath tags.
在 pom.xml 中添加以下插件。检查 mainClass、classpathPrefix、addClasspath 标签处的值。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>org.apache.camel.spring.Main</mainClass>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/assembly/some-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Create some-assembly.xml under src/assembly as below.
在 src/assembly 下创建 some-assembly.xml 如下。
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
Note that useProjectArtifact flag to false, unpack flag to false. If root folder inside zip file is not required,then one can make includeBaseDirectory to false.
注意 useProjectArtifact 标志为 false,unpack 标志为 false。如果不需要 zip 文件中的根文件夹,则可以将 includeBaseDirectory 设置为 false。
This will create name-version-distribution.zip file. Inside zip file, there will be folder name-version. Inside this folder, your executable jar and lib folder containing all dependency jars will be present. Check manifest.MF file of executable jar. It contains both main class and classpath information.
这将创建 name-version-distribution.zip 文件。在 zip 文件中,会有文件夹名称-版本。在此文件夹中,将存在包含所有依赖项 jar 的可执行 jar 和 lib 文件夹。检查可执行 jar 的 manifest.MF 文件。它包含主类和类路径信息。