Maven allows you to easily create Java and Scala projects and build Jar using commands. The advantage of Maven is that you can manage various libraries and resources that depend on the build of your project.
The structure of a typical Maven project is as follows. MavenProject ┣━src ┃ ┣━main ┃ ┃ ┣━java ┃ ┃ ┗━resource ┃ ┗━test ┃ ┣━java ┃ ┗━resource ┗━pom.xml
Locally, you can create the directories needed by Maven commands. For example, create an Mvn Java project.
cd /work
mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false \
-DgroupId=com.demo \
-DartifactId=MvnJava
The created directory is as follows. MvnJava ┣━src ┃ ┣━main ┃ ┃ ┣━java ┃ ┃ ┃ ┗━main ┃ ┃ ┃ ┗━java ┃ ┃ ┃ ┗━com ┃ ┃ ┃ ┗━domo ┃ ┃ ┃ ┗━App.java ┃ ┃ ┗━resource ┃ ┗━test ┃ ┣━java ┃ ┃ ┗━java ┃ ┃ ┗━com ┃ ┃ ┗━domo ┃ ┃ ┗━AppText.java ┃ ┗━resource ┗━pom.xml The contents of App.java are as follows
package com.demo;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
The contents of AppTest.java are as follows
package com.demo;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
pom is an abbreviation of "project object model" and is an object model for handling various information of a project. In pom.xml, the project settings are described as xml tags. Now add a library that depends on the test to do the JUnit test.
<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.demo</groupId>
<artifactId>MvnJava</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MvnJava</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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Compile the Maven project.
cd /work/MvnJava
mvn compile
#Compile is complete when the result below is displayed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
The target directory is created.
Jar, which depends on JUnit tests, is located at:
ls /Users/username/.m2/repository/junit/junit/3.8.1/
junit-3.8.1.jar
mvn test
#result
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.demo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.083 s
[INFO] Finished at: 2019-03-21T16:59:50+09:00
[INFO] ------------------------------------------------------------------------
mvn package
#result
[INFO] Building jar: /work/MvnJava/target/MvnJava-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.768 s
[INFO] Finished at: 2019-03-21T17:12:01+09:00
[INFO] ------------------------------------------------------------------------
MvnJava-1.0-SNAPSHOT.jar is a Jar that can be built.
java -cp /work/MvnJava/target/MvnJava-1.0-SNAPSHOT.jar com.demo.App
#result
Hello World!
mvn -v: | Check the Maven version. Include information about the JDK you are using. |
mvn compile: | Compile the items in Maven. A target directory will be created directly under the Maven directory. The classes directory where the classes are placed is created in it. |
mvn test: | Run Maven tests. To do this, a test class and test results are created in the Target directory. |
mvn package: | Compile the Maven project and build it in the target directory. |
mvn clean: | Delete the target directory. |
mvn install: | Install in your local repository. |
Recommended Posts