[JAVA] Create a Maven project with a command

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.

Maven project structure

The structure of a typical Maven project is as follows. MavenProject ┣━src ┃ ┣━main ┃ ┃ ┣━java ┃ ┃ ┗━resource ┃ ┗━test ┃ ┣━java ┃ ┗━resource ┗━pom.xml

Maven project creation

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 );
    }
}

Edit pom.xml

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 Maven 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	

Implementation of JUnit

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] ------------------------------------------------------------------------

Jar build

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.

Run the jar

java -cp /work/MvnJava/target/MvnJava-1.0-SNAPSHOT.jar com.demo.App 
#result
Hello World!

Maven Frequently used commands

                              
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

Create a Maven project with a command
Create Maven Project
Create command line app with maven
Create a jar file with the command
Create Maven Web Project
Create a Spring Boot app development project with the cURL + tar command
Create a playground with Xcode 12
Create a Jar file with two lines of command
Create a Vue3 environment with Docker!
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
[Rails] Creating a new project with rails new
How to make a Maven project
Create exceptions with a fluid interface
Docker command to create Rails project with a single blow in environment without Ruby
Create a Java (Maven) project with VS Code and develop it on a Docker container
Until you build a project described in scala with Maven and execute it with the scala command.
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
[Rails6] Create a new app with Rails [Beginner]
Create a simple web application with Dropwizard
Create a simple on-demand batch with Spring Batch
Create a GUI JSON Viewer with Ruby/GTK3
Create a MySQL environment with Docker from 0-> 1
Create a simple bar chart with MPAndroidChart
Create a temporary class with new Object () {}
[Java] Create a jar file with both compressed and uncompressed with the jar command
Create a website with Spring Boot + Gradle (jdk1.8.x)
[Memo] Create a CentOS 8 environment easily with Docker
Create a simple search app with Spring Boot
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Rails] rails new to create a database with PostgreSQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
[Java] Create a collection with only one element
Run JUnit and Spock in a maven project
Create a team chat with Rails Action Cable
Create a SandBox account with fastlane spaces ip
[1st] How to create a Spring-MVC framework project
Create a multi-key map with the standard library
Add a project in any folder with Gradle
Automate Java (Maven) project build with CircleCI + Orbs
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Getting started with Maven (until you create a Java project and combine external libraries into a single executable JAR)
Try create with Trailblazer
Create a Java (Gradle) project with VS Code and develop it on a Docker container
[Java] Create a filter
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
[Beginner] Create a competitive game with basic Java knowledge
Create a widget template for iOS14 with Intent Configuration.
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
Create a user with an empty password on CentOS7
[Beginner] I stumbled upon launching a project with Rails6
[Note] Create a java environment from scratch with docker
Create a blog with Jekyll and GitHub Pages @ Theme setting
I tried to create a java8 development environment with Chocolatey
Tutorial to create a blog with Rails for beginners Part 1
Create a page control that can be used with RecyclerView