Maven

Create an empty java project:

mvn archetype:generate -DgroupId=com.example -DartifactId=foobar -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

# Under WINDOWS, double quotes are required:

mvn archetype:generate "-DgroupId=com.example" "-DartifactId=foobar" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DinteractiveMode=false"

Base project structure

foobar
|-- pom.xml
\-- src
  |-- main
  | |-- java
  | | \-- com
  | |   \-- example
  | |     \-- App.java
  | \-- resources
  \-- test
    |-- java
    | \-- com
    |   \-- example
    |     \-- AppTest.java
    \-- resources

To set the Java Version, modify the pom.xml file and add below the </name> tag:

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

Check the dependency tree:

mvn dependency:tree

Download a copy of the dependencies:

mvn dependency:copy-dependencies

Build configuration to specify a main class in manifest file, and run the application using exec:java

<build>
  <finalName>{application name}</finalName>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.4.2</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>{package and class of your main}</mainClass>
            <addClasspath>true</addClasspath>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>3.5.1</version>
      <configuration>
        <mainClass>{package and class of your main}</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

Dealing with failing / not compiling tests

mvn clean install -DskipTests -Dmaven.test.skip=true

Print the version of the current project

mvn help:evaluate -Dexpression=project.version -q -DforceStdout

java

Back