java 如何使用 maven 创建发布包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/796486/
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 to create a release package with maven?
提问by ?ukasz Bownik
I build a desktop application using Maven2.
我使用 Maven2 构建了一个桌面应用程序。
I'd like to make a release from time to time (just copy all the project's and third party jars into a single dir and generate a run.bat file).
我想不时发布一个版本(只需将所有项目和第三方 jar 复制到一个目录中并生成一个 run.bat 文件)。
How to do it ?
怎么做 ?
回答by dogbane
You need to create run.bat yourself and place it in src/main/assembly/scripts, for example. Then you need to create an assembly.xml file in src/main/assembly.
例如,您需要自己创建 run.bat 并将其放置在 src/main/assembly/scripts 中。然后你需要在 src/main/assembly 中创建一个 assembly.xml 文件。
Here is an example of an assembly.xml file that you might want to use. It creates a tar.gz with all your dependency jars and your run.bat.
以下是您可能想要使用的 assembly.xml 文件的示例。它会创建一个 tar.gz,其中包含所有依赖 jar 和 run.bat。
<assembly>
<id>1.0</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/assembly/scripts</directory>
<outputDirectory>/scripts</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Finally, in your pom.xml file add the assembly plugin:
最后,在您的 pom.xml 文件中添加程序集插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when you run "mvn install" you should see your tar.gz created.
现在,当您运行“mvn install”时,您应该会看到已创建的 tar.gz。
To release run:
释放运行:
mvn release:prepare
mvn release:perform
mvn 发布:准备
mvn 发布:执行
回答by ?ukasz Bownik
OK I got it with a little help of dogbane's answer
好的,我在 dogbane 的回答的帮助下得到了它
I used my own assemlby file src/main/assemlby.assembly.xml
我使用了我自己的 assembly 文件 src/main/assemlby.assembly.xml
<assembly>
<id>teleinf</id>
<formats>
<format>dir</format>
</formats>
<moduleSets>
<moduleSet>
<includes>
<include>pl..........:core</include>
<include>pl..........:gui</include>
</includes>
<binaries>
<outputDirectory>../../release</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<fileSet>
<directory>src/main/assembly/</directory>
<outputDirectory>../../release</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
and added following to pom
并在 pom 中添加以下内容
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
I had to write a run.bat myself - so it's not fully satisfying but will do.
我必须自己写一个 run.bat - 所以它不能完全令人满意,但可以。
回答by Michael Pralow
i would go with the maven release plugin, see:
我会使用 Maven 发布插件,请参阅:
回答by Boris Pavlovi?
Just an addition to the answer given by dogbane
只是对dogbane给出的答案的补充
Your .bat file will running packaged jar file with a filename reflecting the current version of the application, e.g.
您的 .bat 文件将运行打包的 jar 文件,其文件名反映了应用程序的当前版本,例如
java -Xms256m -Xmx350m -jar bin\yourApp-1.10.1-SNAPSHOT.jar
On every release this file will need to get updated with a new version name of the application. This can be automatized, too.
在每次发布时,此文件都需要使用应用程序的新版本名称进行更新。这也可以自动化。
In your pom.xml file add this section:
在您的 pom.xml 文件中添加此部分:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.build.sourceDirectory}/../assembly/scripts</directory>
<includes>
<include>yourApp.bat</include>
</includes>
</resource>
...
</resources>
...
</build>
This asumes that you have put the yourApp.bat file in the folder:
这假设您已将 yourApp.bat 文件放在文件夹中:
src/main/assembly/scripts
Content of the yourApp.bat file should look like this:
yourApp.bat 文件的内容应如下所示:
java -Xms256m -Xmx350m -jar bin${project.build.finalName}.jar
Just run the Maven commands and enjoy.
只需运行 Maven 命令并享受。

