Maven2 & Swing projects: Build & run swing applica

2019-03-13 02:11发布

问题:

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.