bookmark_borderMaven Project with CLI

The Apache Maven should be installed in your machine.

mvn --version
# Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
# Maven home: /Library/apache-maven-3.6.3
# Java version: 11.0.8, vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
# Default locale: en_KR, platform encoding: UTF-8
# OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"

Create a maven project with CLI

# mvn archetype:generate -DgroupId=app -DartifactId=prod -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
  • groupId = app
  • artifactId = prod
  • archetypeArtifactId = maven-archetype-quickstart
  • archetypeVersion=1.4
  • interactiveMode=false

The project is created.

package app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}
  • prod/src/main/java/app/App.java
package app;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}
  • prod/src/test/java/app.AppTest.java
<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>app</groupId>
  <artifactId>prod</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>prod</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  • prod/pom.xml

Test Build

mvn test
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.074 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.717 s
[INFO] Finished at: 2020-11-01T00:15:39+09:00
[INFO] ------------------------------------------------------------------------
  • In prod foloder
mkdir -p prod/src/main/java/app
touch prod/src/main/java/app/App.java
mkdir -p prod/src/test/java/app
touch prod/src/test/java/app/AppTest.java
touch prod/pom.xml

Add code to pom.xml, AppTest.java, and App.java

Maven Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project

Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is – you guessed it – a WAR.

An interesting thing to note is that phases and goals may be executed in sequence.

https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

ANOTE.DEV