Reputation: 6774
How do I create a desktop (standalone/Swing) application with Maven?
I'm using Eclipse 3.6.
Upvotes: 18
Views: 62410
Reputation: 106351
The following works for me:
Create a class "App" with a main method
package testswing;
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
JFrame f=new JFrame("Hello World");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Convert to a Maven project (through the Configure... Convert to Maven Project right click menu)
Ensure the pom.xml contains a manifest that specifies your main class:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestSwing</groupId>
<artifactId>TestSwing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>testswing.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run a Maven build with the "package" goal (Run As... Maven Build menu)
Upvotes: 5
Reputation: 33605
Create a Maven project as follows:
mvn archetype:generate -DgroupId=com.yourapp.app
-DartifactId=swingapp
-Dversion=1.0-SNAPSHOT
Add the following entry to your pom file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.yourapp.app.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Import the project into Eclipse as a Maven project, then run as Java application.
Upvotes: 13
Reputation: 42020
UPDATE!
If you get the following error (Apache Maven 3.3.1):
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.3:create
(default-cli) on project standalone-pom: Unable to parse configuration of mojo org.apache
.maven.plugins:maven-archetype-plugin:2.3:create for parameter #: Cannot create instance
of interface org.apache.maven.artifact.repository.ArtifactRepository: org.apache.maven.ar
tifact.repository.ArtifactRepository.<init>() -> [Help 1]
Use the following command:
mvn archetype:generate -DgroupId=com.test -DartifactId=AppTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
You may want to prefer the command line tool for create the project and you may want to prefer eclipse for development.
Create the maven project.
Navigate to the eclipse workspace directory and use the next command line:
mvn archetype:create -DgroupId=com.test -DartifactId=AppTest
Import the project in eclipse:
In menu File > Import..., select Existing Maven Projects:
Input/Browse... the eclipse workspace directory (the directory of the previous step) and select the project:
Enjoy!
Upvotes: 1
Reputation: 23587
archetype used?
A swing application is a standard JAR so just use the standard archetype:
mvn archetype:generate -DgroupId=com.yourapp.app \
-DartifactId=swingapp \
-Dversion=1.0-SNAPSHOT
If you plan to use the standard Swing API only, there aren't no extra dependencies to declare.For extra functionalists you have to use appropriate dependencies in repository
Upvotes: 4