16

How do I create a desktop (standalone/Swing) application with Maven?

I'm using Eclipse 3.6.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
fresh_dev
  • 6,694
  • 23
  • 67
  • 95
  • What exactly are the issues you face? Maven is a build tool it should have little relevance what you are building, more how you are compiling it. Take a look at some of the netbeans tutorials. It's pretty easy to change the projects to use maven when their still small. If its archetypes you are looking for then I'm sure there are a bunch. – Wes Nov 12 '11 at 19:00

4 Answers4

13
  1. Create a Maven project as follows:

    mvn archetype:generate -DgroupId=com.yourapp.app 
                           -DartifactId=swingapp  
                           -Dversion=1.0-SNAPSHOT
    
  2. 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>
    
  3. Import the project into Eclipse as a Maven project, then run as Java application.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
5

The following works for me:

  1. Create a standard Java project
  2. Create a source folder "src/main/java"
  3. Create a package "testswing" in the source folder
  4. 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);
        }  
    }
    
  5. Convert to a Maven project (through the Configure... Convert to Maven Project right click menu)

  6. 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>
    
  7. Run a Maven build with the "package" goal (Run As... Maven Build menu)

  8. You should get an executable .jar file that runs as a standalone Swing application
approxiblue
  • 6,982
  • 16
  • 51
  • 59
mikera
  • 105,238
  • 25
  • 256
  • 415
4
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

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
1

UPDATE!

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

  1. Create the maven project.

    Navigate to the eclipse workspace directory and use the next command line:

    mvn archetype:create -DgroupId=com.test -DartifactId=AppTest
    
  2. Import the project in eclipse:

    In menu File > Import..., select Existing Maven Projects:

    Import Existing Maven Projects

    Input/Browse... the eclipse workspace directory (the directory of the previous step) and select the project:

    Select Maven Projects

    Enjoy!

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148