Thursday, November 20, 2014

Maven: add plugin to pom configuration for Maven to build a jar package in Eclipse and STS

To use maven to compile and build a jar package, create a maven project in Eclipse or STS. After the project is created add the following xml sections to the end of the pom.xml file in the project.

<build>
<plugins>
<plugin>

<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>

<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>

After the implementation of java coding. To build and run using maven, navigate to the project folder in the terminal and run the following command:

> mvn compile exec:java -Dexec.classpathScope=compile -Dexec.mainClass=[mainClassFullPath]

where [mainClassFullPath] refers to the full name of the class containing the main() method.

Or

> mvn clean install