Apache Maven – as build tool : Maven with java Application
Maven with Java Application |
mvn command for creating Basic java project
Syntax :
mvn archetype:generate -DgroupId={project-packaging}
-DartifactId={project-name}
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
It will tell Maven to create a Java project from the Maven maven-archetype-quickstart template. If you ignore the archetypeArtifactId option, a huge list of the Maven templates will be listed.
Example:
mvn archetype:generate -DgroupId=com.javaskool -DartifactId=HelloWorldExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
D:\>md workspaceMaven
D:\>cd workspaceMaven
D:\workspaceMaven>mvn archetype:generate -DgroupId=com.javaskool -DartifactId=HelloWorldExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma
ven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 2.7 KB/s
ec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma
ven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.4 KB/
sec)
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.javaskool
[INFO] Parameter: packageName, Value: com.javaskool
[INFO] Parameter: package, Value: com.javaskool
[INFO] Parameter: artifactId, Value: HelloWorldExample
[INFO] Parameter: basedir, Value: D:\workspaceMaven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\workspaceMaven\HelloW
orldExample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.753 s
[INFO] Finished at: 2015-02-19T12:27:15+05:30
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------
D:\workspaceMaven>
Here, a new Java project named “HelloWorldExample” would get created, and as well as the entire project directory structure is created automatically as below.
Here, all source code puts in folder /src/main/java/, and all unit test code puts in /src/test/java/.
A standard pom.xml is generated. This POM file is like the Apache Ant build.xml file.
It describes the entire project information, everything from directory structure, project plugins, project dependencies, how to build this project and etc.
You can read more about pom.xml on this official POM guide.
pom.xml file create by above command
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaskool</groupId>
<artifactId>HelloWorldExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HelloWorldExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Making Eclipse Project.
To make this as an Eclipse project, type below command on terminal :
Syntax :
mvn eclipse:eclipse
<em>D:\workspaceMaven>cd HelloWorldExample</em>
<em>D:\workspaceMaven\HelloWorldExample>dir</em>
Volume in drive D has no label.
Volume Serial Number is C601-072F
Directory of D:\workspaceMaven\HelloWorldExample
19-02-2015 12:27 <DIR> .
19-02-2015 12:27 <DIR> ..
19-02-2015 12:27 678 pom.xml
19-02-2015 12:27 <DIR> src
1 File(s) 678 bytes
3 Dir(s) 57,250,795,520 bytes free
<em>D:\workspaceMaven\HelloWorldExample>mvn eclipse:eclipse</em>
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom (11 KB at 3.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/sure
fire/2.12.4/surefire-2.12.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/suref
ire/2.12.4/surefire-2.12.4.pom (14 KB at 24.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.jar (30 KB at 32.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-jar-plugin/2.4/maven-jar-plugin-2.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
jar-plugin/2.4/maven-jar-plugin-2.4.jar (34 KB at 23.8 KB/sec)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorldExample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) > generate-resources @
HelloWorldExample >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) < generate-resources @
HelloWorldExample <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ HelloWorldExample --
-
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-uti
ls/1.4.6/plexus-utils-1.4.6.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-util
s/1.4.6/plexus-utils-1.4.6.pom (3 KB at 2.4 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/eclipse/core/resources/3.3.
0-v20070604/resources-3.3.0-v20070604.jar (663 KB at 56.2 KB/sec)
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAIN
ER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "HelloWorldExample" to D:\workspaceMaven\HelloW
orldExample.
[INFO]
[INFO] ------------------------------------------------------------------------
<em>[INFO] BUILD SUCCESS</em>
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 49.734 s
[INFO] Finished at: 2015-02-19T13:26:26+05:30
[INFO] Final Memory: 9M/22M
[INFO] ------------------------------------------------------------------------
D:\workspaceMaven\HelloWorldExample>
After Running above command, Your project directory structure as below
It will help you to generate all project files that are required by Eclipse IDE. Now, you can open eclipse and import this project. To import the project into Eclipse IDE, select "File ->> Import… ->> General ->> Existing Projects into Workspace"
And then browse for the “D:\workspaceMaven\HelloWorldExample” folder as per this example. And your Maven java project is reddy in the eclipse as below.
You can update the java version 1.6 and JUnit versions by using below code in pom.xml. Update the JUnit from 3.8.1 to latest 4.11.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaskool</groupId>
<artifactId>HelloWorldExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HelloWorldExample</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
After updating this file either you save the file in eclipse and it will help you to download all updted dependencies or you can enter the same command [mvn eclipse:eclipse] again on terminal. Below is the Eclipse project with updated dependencies.
Lets update our application code and test code.
In this code i want to test the method whether it returns name with Hello.
It is good to write Test first, I mean you can have TDD(Test Driven Development).
AppTest.java
package com.javaskool;
import org.junit.Assert;
import org.junit.Test;
public class AppTest
{
@Test
public void testLengthOfTheUniqueKey() {
App app = new App();
Assert.assertEquals("Hello James Bond", app.displayMessage("James Bond"));
}
}
App.java
package com.javaskool;
public class App
{
public static void main( String[] args )
{
App app=new App();
System.out.println(app.displayMessage("James Bond"));
}
public String displayMessage(String name)
{
return "Hello "+name;
}
}
Maven Packaging
Now, we will use Maven to compile this project and output to a “jar” file. Refer to the pom.xml file, the packaging element defined what should be the packaging output.
Right click on pom.xml and then select Run As and then Maven Build. So it will build and run the test.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorldExample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ HelloWorldExample ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\workspaceMaven\HelloWorldExample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ HelloWorldExample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ HelloWorldExample ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\workspaceMaven\HelloWorldExample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ HelloWorldExample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ HelloWorldExample ---
[INFO] Surefire report directory: D:\workspaceMaven\HelloWorldExample\target\surefire-reports
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.pom (3 KB at 0.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.jar (33 KB at 32.3 KB/sec)
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.javaskool.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.204 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.237s
[INFO] Finished at: Thu Feb 19 14:07:26 IST 2015
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
Also Using Terminal through mvn package command
D:\workspaceMaven\HelloWorldExample>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorldExample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld
Example ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-
filtering/1.1/maven-filtering-1.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-f
iltering/1.1/maven-filtering-1.1.pom (6 KB at 1.8 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/suref
ire-providers/2.12.4/surefire-providers-2.12.4.pom (3 KB at 4.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/sure
fire-junit4/2.12.4/surefire-junit4-2.12.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/suref
ire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 KB at 43.9 KB/sec)
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.javaskool.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.518 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloWorldExample ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archive
r/2.5/maven-archiver-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-arch
iver/2.1/plexus-archiver-2.1.jar (181 KB at 69.1 KB/sec)
[INFO] Building jar: D:\workspaceMaven\HelloWorldExample\target\HelloWorldExampl
e-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 36.681 s
[INFO] Finished at: 2015-02-19T14:12:04+05:30
[INFO] Final Memory: 7M/20M
[INFO] ------------------------------------------------------------------------
D:\workspaceMaven\HelloWorldExample>
Let’s Run the App
D:\workspaceMaven\HelloWorldExample>cd target
D:\workspaceMaven\HelloWorldExample\target>dir
Volume in drive D has no label.
Volume Serial Number is C601-072F
Directory of D:\workspaceMaven\HelloWorldExample\target
19-02-2015 14:12 <DIR> .
19-02-2015 14:12 <DIR> ..
19-02-2015 14:04 <DIR> classes
19-02-2015 14:12 2,506 HelloWorldExample-1.0-SNAPSHOT.jar
19-02-2015 14:12 <DIR> maven-archiver
19-02-2015 14:07 <DIR> surefire-reports
19-02-2015 14:04 <DIR> test-classes
1 File(s) 2,506 bytes
6 Dir(s) 56,406,720,512 bytes free
D:\workspaceMaven\HelloWorldExample\target>java -cp HelloWorldExample-1.0-SNAPSHOT.jar com.javaskool.App
Hello James Bond
D:\workspaceMaven\HelloWorldExample\target>
Downloads Example |
Recent Comments