Maven2 & Swing projects: Build & run swing application

只谈情不闲聊 提交于 2019-12-03 09:28:42

问题


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).

Can someone point me to relevant documentation? Is anyone using maven in swing development ?


回答1:


I'm guessing that you want to run your app from a maven command. You can use the exec plugin like this:

<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.

<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.

Just for completeness, here's some sample code to make an executable jar file to go with the link in romaintaz's answer.

<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>



回答2:


What exactly do you want to achieve?

A Swing application is a "normal" Java application, so without specific needs regarding the Maven configuration.

You can have a look here to know how to create a runnable JAR file with Maven. You can also have a look here in order to create a JAR file that contains all dependencies.



来源:https://stackoverflow.com/questions/681235/maven2-swing-projects-build-run-swing-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!