简体   繁体   English

Maven2&Swing项目:构建和运行swing应用程序

[英]Maven2 & Swing projects: Build & run swing application

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 ? 是否有人在摇摆开发中使用maven?

I'm guessing that you want to run your app from a maven command. 我猜你想从maven命令运行你的应用程序。 You can use the exec plugin like this: 您可以像这样使用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. 实际配置可能会有所不同,具体取决于您最终使用的exec插件的版本 - 我已经在某些版本上取得了成功,但在其他版本上没有成功,因此找出正确的版本是一种试验和错误。 jar为您的项目。 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. 如果你有多个开发人员,这也是一种痛苦,因为一个开发者的参数可能对另一个开发者来说不正确,所以最好只编写一个批处理/ 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>

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 here to know how to create a runnable JAR file with Maven. 您可以在这里了解如何使用Maven创建可运行的JAR文件。 You can also have a look here in order to create a JAR file that contains all dependencies. 您还可以在此处查看以创建包含所有依赖项的JAR文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM