java Maven2 & Swing 项目:构建和运行 Swing 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/681235/
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
Maven2 & Swing projects: Build & run swing application
提问by javito
I tried to find info on how to use maven to build and run a swing application but couldn't find anything useful (maven documentation is a mess).
我试图找到有关如何使用 maven 构建和运行 Swing 应用程序的信息,但找不到任何有用的信息(maven 文档一团糟)。
Can someone point me to relevant documentation? Is anyone using maven in swing development ?
有人可以指点我相关的文件吗?有人在 Swing 开发中使用 Maven 吗?
回答by Matt McMinn
I'm guessing that you want to run your app from a maven command. You can use the execplugin like this:
我猜您想从 Maven 命令运行您的应用程序。您可以像这样使用exec插件:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1-beta-1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.package.MainClass</mainClass>
<arguments>
<argument>arg1</argument>
<argument>arg2</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
You may need this in your pom as well.
你可能也需要在你的 pom 中使用它。
<repositories>
<repository>
<id>Maven Snapshots</id>
<url>http://snapshots.maven.codehaus.org/maven2/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>Maven Snapshots</id>
<url>http://snapshots.maven.codehaus.org/maven2/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
The actual configuration may vary, depending on which version of the exec plugin you actually end up using - I've had success with some versions, but no success with others, so it's kind of trial and error to figure out the right version of the jar for your project. It's also kind of a pain if you have multiple developers, as arguments for one dev may not be correct for another, so it may be better just writing a batch/shell script to start the app.
实际配置可能会有所不同,具体取决于您最终使用的 exec 插件的版本 - 我在某些版本上取得了成功,但在其他版本上没有成功,因此找出正确版本的jar 为您的项目。如果您有多个开发人员,这也是一种痛苦,因为一个开发人员的参数可能对另一个开发人员不正确,因此最好编写一个批处理/shell 脚本来启动应用程序。
Just for completeness, here's some sample code to make an executable jar file to go with the link in romaintaz's answer.
为了完整起见,这里有一些示例代码可以制作一个可执行的 jar 文件,以与 romaintaz 的答案中的链接一起使用。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
回答by Romain Linsolas
What exactly do you want to achieve?
你究竟想达到什么目的?
A Swing application is a "normal" Java application, so without specific needs regarding the Maven configuration.
Swing 应用程序是一个“普通的”Java 应用程序,因此没有关于 Maven 配置的特定需求。
You can have a look hereto know how to create a runnable JAR file with Maven. You can also have a look herein order to create a JAR file that contains all dependencies.
您可以查看此处了解如何使用 Maven 创建可运行的 JAR 文件。您还可以查看此处以创建包含所有依赖项的 JAR 文件。

