java maven - ghdrako/doc_snipets GitHub Wiki

maven

Maven uses Convention over Configuration, which means developers are not required to create build process themselves. Developers do not have to mention each and every configuration detail. Maven provides sensible default behavior for projects.

Item Default
source code ${basedir}/src/main/java
Resources ${basedir}/src/main/resources
Tests ${basedir}/src/test
Complied byte code ${basedir}/target
distributable JAR ${basedir}/target/classes

Standard directory layout

Super POM

The Super POM is Maven’s default POM. All POMs inherit from a parent or default (despite explicitly defined or not). This base POM is known as the Super POM, and contains values inherited by default.

Maven use the effective POM (configuration from super pom plus project configuration) to execute relevant goal. It helps developers to specify minimum configuration detail in his/her pom.xml. Although configurations can be overridden easily.

mvn help:effective-pom

Plugins

Goal

Goals in Maven are packaged in plug-ins, which are essentially a collection of one or more goals. in example the compiler is the plug-in that provides the goal compile. The clean goal cleaned out of the target folder from Maven-generated temporary files and artifacts. The format of the command clean:clean The clean before the colon (:) represents the clean plug-in, and the clean following the colon represents the clean goal.

Running a goal in the command line requires the following syntax:

mvn plugin_identifier:goal_identifier  

Maven provides an out-of-the box Help plug-in that can be used to list available goals in a given plug-in.

mvn help:describe -Dplugin=compiler

Plug-ins and their behavior can be configured using the plug-in section of pom.xml.

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>

Now if you were to run the mvn compiler:compile command, the generated class files will be of Java version 1.8.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wellgrounded</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>example</name>
  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
   </properties>
</project>

Lifecycle

Maven’s build lifecycle constitutes a series of stages that get executed in the same order, independent of the artifact being produced. Maven refers to the stages in a lifecycle as phases. Every Maven project has the following three built-in lifecycles:

  • default: This lifecycle handles the compiling,packaging, and deployment of a Maven project.
  • clean: This lifecycle handles the deletion of temporary files and generated artifacts from the target directory.
  • site: This lifecycle handles the generation of documentation and site generation.

Default lifecycle phase:

  • validate: Runs checks to ensure that the project is correct and that all dependencies are downloaded and available.
  • compile: Compiles the source code.
  • test: Runs unit tests using frameworks. This step doesn’t require that the application be packaged.
  • package: Assembles compiled code into a distributable format, such as JAR or WAR.
  • install: Installs the packaged archive into a local repository. The archive is now available for use by any project running on that machine.
  • deploy: Pushes the built archive into a remote repository for use by other teams and team members.

Maven lifecycle is an abstract concept and can’t be directly executed. Instead, you execute one or more phases. For example, the command mvn package will execute the package phase of the default lifecycle. In addition to clearly defining the ordering of phases in a lifecycle, Maven also automatically executes all the phases prior to a requested phase. So, when the mvn package command is run, Maven will run all prior phases such as compile and test.

A number of tasks need to be performed in each phase. For that to happen, each phase is associated with zero or more goals. The phase simply delegates those tasks to its associated goals.

Plugins

Maven provides near 50 “built-in” plugins, split in four different categories:

  • core plugins, like clean, compiler, install, deploy, site, surefire (test), …
  • packaging plugins, like jar, war, shade, source, jlink, jmod, …
  • reporting plugins, like javadoc, jxr, checkstyle, pmd, jdeps, …
  • and many tools plugins, like archetype, assembly, enforcer, dependency, release, antrun, scripting, …

Archetypes

The archetype plug-in’s generate goal allows you to view and select an archetype for use.

$mvn archetype:generate

Example: Generating a Web Project

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
mvn archetype:generate -DgroupId=com.example -DartifactId=my-rest-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Commands

mvn <lifecycle phase or goal>
 mvn compile  # requesting compile phase
 mvn test-compile
 mvn package  

missing a manifest in jar to tell the JVM where to look for a main method on startup so

java -jar target/ example-1.0-SNAPSHOT.jar

not work

To fix:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.wellgrounded.Main</mainClass>
<Automatic-Module-Name>
com.wellgrounded
</Automatic-Module-Name>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Show dependency - to debug problem with dependency conflict

mvn dependency:tree

Build project

mvn clean package

settings.xml

Maven looks for the settings.xml file in two locations – in the conf folder of Maven’s installation and .m2 folder in the user’s home directory. The settings.xml file under conf folder is called global settings, and the file under .m2 folder is referred to as user settings. If both files exist, Maven will merge the contents of two files and the user settings will take precedence.

.m2 folder

By default, the .m2 folder is located in your home directory. In Windows, this directory is usually c:\Users<<your_user_ name>>.

 mvn help:evaluate -Dexpression=settings.localRepository
<settings>
    <localRepository>C:/maven_repository</localRepository>
    ...

Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.9</version>
    </dependency>
     <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>2.0-M5-groovy-3.0</version>
        <scope>test</scope>                        # tylko w testach nie dodana do pakietu
    </dependency>
</dependencies>

Build

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
⚠️ **GitHub.com Fallback** ⚠️