Java maven: projects

From wikinotes


Project Structure

source-code ${basedir}/src/main/java/
resources ${basedir}/src/main/resources/
tests ${basedir}/src/test/
compiled ${basedir}/target/
jarfile ${basedir}/target/classes/


project/
    pom.xml
    target/
    src/
        main/
            java/
            resources/
            webapp/
        test/
            java/
            resources/

http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Create Project

NOTE:

I wrote a mvn-create python script that simplifies project creation.


Setup Environment

You should tell maven/java which JDK version to use.

export JAVA_HOME=/usr/lib/jvm/default  # or 'java-11-openjdk', 'java-11-jdk', ...

Create Empty Project


Creates pom.xml, project-hierarchy, and a HelloWorld class to test with.

mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.mycompany.app    `# inverted-domain.your-package  (ex: app.mycompany.com) `\
  -DartifactId=my_app            `# name of jarfile`

This will generate a directory structure, and pom.xml that looks something like this

<?xml version="1.0" ?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my_app</artifactId>
    <name>my_app</name>
</project>


Set maven compiler versions (if necessary)


Run the following, and check for errors.

mvn package

If an error like the following is raised, you will need to add additional configuration to your pom.xml.

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.


Correction to pom.xml:

<project ...>
  <properties>
    <maven.compiler.source>8</maven.compiler.source>    <!-- installed java -->
    <maven.compiler.target>1.8</maven.compiler.target>  <!-- minimum-required-java -->
  </properties>
</project>

See  https://mincong-h.github.io/2018/08/29/maven-compiler-plugin-understanding/#choose-java-version

* compiler.source is 8 for functional programming
* compiler.target (Execution-Environment) is 1.8 for functional programming


Set Text-Encoding

In order for your project to be cross-platform, you need to be explicit about the text-encoding you want to use.

pom.xml

<project ...>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>


configure maven-jar-plugin (main-class, and version)


version
Check the maven repo, and Explicitly set the plugin version for maven. (ex: '3.1.1'). It must be explicit (3, for example is not sufficient).

https://mvnrepository.com/search?q=maven-jar-plugin


mainclass
If your jarfile is intended as an executable, you need to inform java what code is executed when it is run directly on the commandline. Normally this is done by hand, but here we are configuring maven's jar-plugin.

pom.xml

<project ...>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.willpittman.maven_intro.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Quickly build and run, to verify everything worked


mvn package                          # build jar
java -jar target/package-1.0.0.jar   # run jarfile