Search

Dark theme | Light theme
Showing posts with label MasteringMaven:CLI. Show all posts
Showing posts with label MasteringMaven:CLI. Show all posts

November 3, 2024

Mastering Maven: Disable Logging Of Progress Downloading Artifacts

When Maven needs to download artifacts from a remote repository, it logs the progress of the download. This can lead to a lot of noise in the output. Luckily, we can suppress the logging of the download progress. Since Maven 3.6.1. we can use the command-line option --no-transfer-progress to disable the logging of the download progress. There is also a short version of the option: -ntp.

First look at an example where we do not disable the logging of the download progress. In the output, we see (a lot of) messages showing which artifacts are downloaded and the progress.

$ mvn package
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/3.3.5/spring-boot-starter-parent-3.3.5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/3.3.5/spring-boot-starter-parent-3.3.5.pom (13 kB at 29 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/3.3.5/spring-boot-dependencies-3.3.5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/3.3.5/spring-boot-dependencies-3.3.5.pom (100 kB at 1.7 MB/s)
...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/3.3.5/spring-boot-maven-plugin-3.3.5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/3.3.5/spring-boot-maven-plugin-3.3.5.pom (4.0 kB at 308 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/3.3.5/spring-boot-maven-plugin-3.3.5.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/3.3.5/spring-boot-maven-plugin-3.3.5.jar (137 kB at 6.5 MB/s)
...

We remove the downloaded dependencies from our local repository and run the same Maven command again, but now we add the command-line option --no-transfer-progress (or the short version -ntp). We no longer have the downloading progress messages in our output:

$ mvn --no-transfer-progress package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...

Instead of adding this option each time we run Maven, we can add it to the file .mvn/maven.config in our project:

--no-transfer-progress

Alternatively, since Maven 3.9.0, we can add the option to the environment variable MAVEN_ARGS.

$ export MAVEN_ARGS=-ntp 
$ mvn package
[INFO] Scanning for projects...
[INFO]
...

Written with Maven 3.9.9.

April 6, 2023

Mastering Maven: Adding Maven Extensions Using extensions.xml

A .mvn directory in the root of our project can contains some useful extras. For example we can set default Maven options or Java VM options when we run a Maven command. We can also define Maven extensions we want to add to the Maven classpath using the file extensions.xml in the .mvn directory. The Maven extension we want to add can be referenced using a groupId, artifactId and version inside an <extension> element. We can define one or more extensions within the parent element extensions. Once we have defined the extension and run Maven the extension is added to classpath of Maven itself.

In the following example we apply the Maven Enforcer extension to our project. We add the file extensions.xml in the .mvn directory that we first have to create:

<!-- File: .mvn/extensions.xml -->
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.1.0 http://maven.apache.org/xsd/core-extensions-1.1.0.xsd">
    <!-- Add Maven Enforcer extension -->
    <extension>
        <groupId>org.apache.maven.extensions</groupId>
        <artifactId>maven-enforcer-extension</artifactId>
        <version>3.3.0</version>
    </extension>
</extensions>

This extension requires an extra configuration file with rules we want to apply to our project. The filename must be enforcer-extension.xml and placed in the .mvn directory as well. In our example we add a rule that checks that the Java version we use to run Maven is at least version 17. And a rule to check that our Maven version is at least 3.9.1:

<!-- File: .mvn/enforcer-extension.xml -->
<extension>
    <executions>
        <execution>
            <id>maven-enforcer-extension</id>
            <phase>validate</phase>
            <configuration>
                <rules>
                    <!-- Our build requires Java 17 or higher -->
                    <requireJavaVersion>
                        <version>17</version>
                    </requireJavaVersion>
                    <!-- Use Maven 3.9.1 or higher -->
                    <requireMavenVersion>
                        <version>3.9.1</version>
                    </requireMavenVersion>
                </rules>
            </configuration>
        </execution>
    </executions>
</extension>

Next we run the package command with Java version 11.0.17 and we see in the output that the Enforcer extension failed because of the required Java version rule:

$ mvn package
...
[INFO] --- enforcer:3.3.0:enforce (maven-enforcer-extension) @ sample ---
[INFO] Rule 1: org.apache.maven.enforcer.rules.version.RequireMavenVersion passed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.777 s (Wall Clock)
[INFO] Finished at: 2023-04-06T17:19:16+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.3.0:enforce (maven-enforcer-extension) on project sample:
[ERROR] Rule 0: org.apache.maven.enforcer.rules.version.RequireJavaVersion failed with message:
[ERROR] Detected JDK version 11.0.17 (JAVA_HOME=/Users/mrhaki/.sdkman/candidates/java/11.0.17-tem) is not in the allowed range [17,).
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
$

Written with Maven 3.9.1.

April 4, 2023

Mastering Maven: Setting Default Maven Options With maven.config

In a previous blog we learned about setting default JVM options when we run Maven commands. We can also set default Maven options that we want to apply each time we run a Maven command. All options can be defined in the file maven.config in a .mvn directory in the root of our project. Each option must be defined on a new line. This directory and file can be added to our source control so that all users that have access to the repository will use the same Maven options.

In the following example we have a file .mvn/maven.config where we want to show Maven version information for each run, use a thread per CPU core, always try to build all modules and fail at the end if there are failures and finally we set user properties that are used by the Maven compile plugin:

--show-version
--threads=1C
--fail-at-end
-Dmaven.compiler.source=17
-Dmaven.compiler.target=17
-Dencoding=UTF-8

With the above configuration we can see that options are applied when we run the verify command:

$ mvn verify
...
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: /Users/mrhaki/.sdkman/candidates/maven/current
Java version: 19.0.2, vendor: Eclipse Adoptium, runtime: /Users/mrhaki/.sdkman/candidates/java/19.0.2-tem
Default locale: en_NL, platform encoding: UTF-8
OS name: "mac os x", version: "13.2.1", arch: "x86_64", family: "mac"
...
[INFO] Scanning for projects...
[INFO]
[INFO] Using the MultiThreadedBuilder implementation with a thread count of 12
...
$

Written with Maven 3.9.1.

March 26, 2023

Mastering Maven: Setting Default JVM Options Using jvm.config

In order to add default JVM options to our Maven mvn command we can define an environment variable MAVEN_OPTS. But we can also create a file jvm.config in the directory .mvn in our project root directory. On each line we define a Java option we want to apply. We can specify JVM options, but also Java system properties we want to apply each time we run the mvn command. This directory and file can be added to our source control so that all users that have access to the repository will use the same JVM options.

The following example defines some Java system properties to change the Maven logging. We also add the JVM option --show-version, so that each time we run mvn we also see the version of Java that is used to run our build. We create the file .mvn/jvm.config and the following content:

-Dorg.slf4j.simpleLogger.showDateTime=true
-Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss
-Dorg.slf4j.simpleLogger.showThreadName=true
--show-version

Now when we run mvn we see in the output the Java version displayed and each log line has a time and thread name:

$ mvn verify
openjdk 19.0.2 2023-01-17
OpenJDK Runtime Environment Temurin-19.0.2+7 (build 19.0.2+7)
OpenJDK 64-Bit Server VM Temurin-19.0.2+7 (build 19.0.2+7, mixed mode, sharing)
22:12:35 [main] [INFO] Scanning for projects...
...
22:12:39 [main] [INFO] ------------------------------------------------------------------------
22:12:39 [main] [INFO] BUILD SUCCESS
22:12:39 [main] [INFO] ------------------------------------------------------------------------
22:12:39 [main] [INFO] Total time:  3.844 s
22:12:39 [main] [INFO] Finished at: 2023-03-26T22:12:39+02:00
22:12:39 [main] [INFO] ------------------------------------------------------------------------
$

Written with Maven 3.9.1

November 18, 2022

Mastering Maven: Exclude Modules From Build

When we are working with a multi-module project in Maven we might want to exclude a module when we invoke a build command. We might only be interested in partially building some modules. We can use the command line option -pl or --projects to specify a list of modules that need to be in our build. But we can also use ! followed by the module name to exclude modules from our build.

In the following example we have multi-module project with 4 modules: root with name parent-project, core, service and webapp:

$ pwd
parent-project
$ tree -L 1
.
├── core
├── pom.xml
├── service
└── webapp
$

First we run verify for the complete project and we see in the output that the reactor build order contains all modules:

$ mvn verify
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO] webapp                                                             [war]
[INFO]
...
$

Suppose we want to exclude our webapp module than we use $ mvn -pl '!webapp' verify. In the output we see the reactor build order doesn't include our webapp module:

$ mvn verify -pl '!webapp'
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO]
...
$

As our list of modules is not so big in this sample project we could also have used the option -pl to only specify the modules we want to build separated by a comma. We must use . to indicate our root module:

$ mvn verify -pl '.,core,service'
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO]
...
$

Written with Maven 3.8.6.