fresh_dev
fresh_dev

Reputation: 6774

Create a standalone application with Maven

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

I'm using Eclipse 3.6.

Upvotes: 18

Views: 62410

Answers (4)

mikera
mikera

Reputation: 106351

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

Upvotes: 5

Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

  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.

Upvotes: 13

Paul Vargas
Paul Vargas

Reputation: 42020

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!

Upvotes: 1

Umesh Awasthi
Umesh Awasthi

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

Related Questions