Thursday, 24 May 2018

Hello World with Maven

Getting started with Maven is slightly complicated, so I thought I'd write down some links1 2 on how to get up and running quickly with a simple HelloWorld Java application with Maven archetypes.

That and I wanted to tell how to upgrade the Java version used for the Maven project. There seem to be different ways to do it. Some easy, some hard.

Groupidorg.apache.maven
Artifactidthe name of the jar without version, for example maven, commons-math
Version2.0
mvn archetype:generate -DgroupId=com.mrbear -DartifactId=mrbear -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Verify that this is the latest archetype version4.

Upgrading to Java 8

One way is to add the versions to the properties in the pom.xml, which is easiest.

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

Another way is to change the maven compiler plugin to use the new version, which is harder.

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

References

[1] Apache Maven Project - Creating a simple java application
https://maven.apache.org/plugins-archives/maven-archetype-plugin-1.0-alpha-7/examples/simple.html
[2] Apache Maven Project - Maven in 5 Minutes
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
[3] Apache Maven Project - Setting the -source and -target of the Java Compiler
https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
[4] Apache - Maven Archetype Quickstart Summary
http://maven.apache.org/archetypes/maven-archetype-quickstart/summary.html

No comments:

Post a Comment