[JAVA] A small story that is sometimes useful in Maven

It is a small story that is often useful when actually working.

Small story at build time

I think everyone often skips running tests with -DskipTests at build time (?).

Similarly, you can skip the test execution with -Dmaven.test.skip, but this also skips the compilation of the test code, so it's okay if the test code has a compilation error (??).

Furthermore, if the test scope dependency is broken even after executing mvn package -Dmaven.test.skip (dependency cannot be resolved. In rare cases, such as a project that refers to a repository other than Maven Central. Yes) will result in a build error. In that case, do not mvn package, but mvn compile jar: jar (if packaging is war, war: war instead of jar: jar) will make the test scope dependency. It's okay because it builds without solving it (???).

Small story around the investigation when the build fails

If the -e option is added, a stack trace will be output when the build fails. Experience shows that Maven often cannot be identified immediately by looking at the stack trace, but it's better than nothing, so basically it's a good idea to add the -e option.

Then add the -X option and the debug log will be output. Since a large amount of logs are output and it is difficult to go back to the console, it is recommended to write to a log file together with the -l option.

It's like this.

mvn -X -l maven.log test

If you want to read Maven code during the investigation, you can get it from https://github.com/apache/maven.

Small story around authentication at the time of deployment

When deploying a JAR or WAR to a Maven repository such as Nexus, Maven reads the credentials of the server to which it is deployed from settings.xml.

When deploying with CI/CD tool, put the following settings.xml in the same location as the source code and specify it with the -s option (command example mvn -s settings.xml deploy). There is.

<settings>
    <servers>
        <server>
            <id>nexus</id>
            <username>deployuser</username>
            <password>deploypassword</password>
        </server>
    </profiles>
</settings>

If you don't want to hard-code the password, you can read it from the environment variable by setting the password element as follows.

<password>${env.DEPLOY_PASSWORD}</password>

In this case, read the password from the environment variable DEPLOY_PASSWORD. Some CI/CD tools set secret information in environment variables at build time, but this is a method that can be used in such an environment.

You can also encrypt your password if you don't want to hardcode it in your local settings.xml. See the Password Encryption (https://maven.apache.org/guides/mini/guide-encryption.html) section of the official Maven documentation for more information.

Small story around help

You can get help for the mvn command with mvn --help, but you can list the goals of the plugin with mvn <plugin>: help. For example, running mvn compiler: help will give you the following output:

[INFO] Apache Maven Compiler Plugin 3.8.0
  The Compiler Plugin is used to compile the sources of your project.

This plugin has 3 goals:

compiler:compile
  Compiles application sources

compiler:help
  Display help information on maven-compiler-plugin.
  Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

compiler:testCompile
  Compiles application test sources.

You can also see more details about a particular goal, as described in compiler: help. You can see the parameter description in the goal details.

Useful when writing plugin settings ... I don't know which is faster and more convenient to google the plugin and get to the official docs!

Also, maven-help-plugin may be useful, so I recommend keeping the existence in the corner of your head (I'm exhausted, so I'll omit the explanation ...).

in conclusion

When I'm actually moving my hands, I feel like I'm doing more, but for the time being, this is the small story I can remember when I'm relaxing.

that's all.

Recommended Posts

A small story that is sometimes useful in Maven
The story that .java is also built in Unity 2018
Determine that the value is a multiple of 〇 in Ruby
A story that says Table doesn't exist even though there is a table
What is a snippet in programming?
The story that Tomcat suffered from a timeout error in Eclipse
A little troublesome story in Groovy
A story about a Spring Boot project written in Java that supports Kotlin
[Java small story] Monitor when a value is added to the List
[Small story] Misleading method name (a story that wasted time due to setScale.
A story about an arithmetic overflow that you shouldn't encounter in Ruby
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Methods that I found useful in Ruby
[Java] Difference between equals and == in a character string that is a reference type
A story about the JDK in the Java 11 era
A story that separates business logic and model
The story that docker had a hard time
Ruby sum is a sober and amazing story
A story about a very useful Ruby Struct class
Run JUnit and Spock in a maven project
The story that link_to is deep (cause unknown)
A story about making a Builder that inherits the Builder
A bat file that uses Java in windows
A memo for myself that object-oriented is something
A story that failed using "bundle exec rubocop -a"
A description of Interface that is often mistaken
A story that got stuck with an error during migration in docker PHP laravel
Java Error Handling Basics-The story that catch is only picked up in the foreground
A story that did not work when trying to handle events in Notification Center
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war