Converting a Maven Project to Create a Runnable JAR

See www.petrikainulainen.net for more information.

In my pom.xml file, I changed build tag to include the “Maven Assembly Plugin”.
Only had to edit the main class value, based on the package and class name.


	<build>
		<plugins>             <!-- The configuration of maven-assembly-plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2.2</version>                 <!-- The configuration of the plugin -->
				<configuration>                     <!-- Specifies the configuration file of the assembly plugin -->
					<descriptors>
						<descriptor>src/main/assembly/assembly.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>             <!-- The configuration of maven-jar-plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.1</version>                 <!-- The configuration of the plugin -->
				<configuration>                     <!-- Configuration of the archiver -->
					<archive>                         <!-- Manifest specific configuration -->
						<manifest>                             <!-- Classpath is added to the manifest of the created jar file. -->
							<addClasspath>true</addClasspath>                             <!-- Configures the classpath prefix. This configuration option is used to 
								specify that all needed libraries are found under lib/ directory. -->
							<classpathPrefix>lib/</classpathPrefix>                             <!-- Specifies the main class of the application -->
							<mainClass>com.main.SwingApp</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build> 

The main class was from this file:


package com.main;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    private static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? "+
                SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing App");
        
        
        
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(250,250);
        f.setVisible(true);
    }

}

Then, added the default assembly file, creating a new source folder “src/main/assembly” and adding the “assembly.xml” file (reposted from www.petrikainulainen.net)


<assembly>
	<id>bin</id>     <!-- Generates a zip package containing the needed files -->
	<formats>
		<format>zip</format>
	</formats>       <!-- Adds dependencies to zip package under lib directory -->
	<dependencySets>
		<dependencySet>             <!-- Project artifact is not copied under library directory since it is added 
				to the root directory of the zip package. -->
			<useProjectArtifact>false</useProjectArtifact>
			<outputDirectory>lib</outputDirectory>
			<unpack>false</unpack>
		</dependencySet>
	</dependencySets>
	<fileSets>         <!-- Adds startup scripts to the root directory of zip package. The startup 
			scripts are located to src/main/scripts directory as stated by Maven conventions. -->
		<fileSet>
			<directory>${project.build.scriptSourceDirectory}</directory>
			<outputDirectory></outputDirectory>
			<includes>
				<include>startup.*</include>
			</includes>
		</fileSet>         <!-- adds jar package to the root directory of zip package -->
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory></outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly> 

Then, after issuing ‘mvn package’ from Git Bash command prompt in the same directory as the pom.xml file, it created the target directory with a runnable jar. I double clicked the jar to test.

SWING
Help – Install New Software – Copy http://download.eclipse.org/releases/juno into field – Add
http://download.eclipse.org/windowbuilder/WB/integration/4.4/
Capture

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s