Create Java project using maven

Here we will create a project using maven template and import the project in eclipse.
Technology used
  • Java 18.0.1.1 2022-04-22
  • Apache Maven 4.0.0-alpha-3
  • OS: MacOS Monterey (M1)
  • Eclipse IDE

Create project

Using maven we will generate the project using archetype = maven-archetype-quickstart. archetype is predefined templates. And then we will generate the eclipse related config files so that the generate project can be imported in eclipse.
Console : Command
mvn archetype:generate -DgroupId=com.project -DartifactId=java-module-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  
Console : Output
$ mvn archetype:generate -DgroupId=com.project -DartifactId=java-module-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------------------------< org.apache.maven:standalone-pom >-----------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] -------------------------------------------------------[ pom ]-------------------------------------------------------
[INFO] 
[INFO] --- archetype:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/sbhome
[INFO] Parameter: package, Value: com.project
[INFO] Parameter: groupId, Value: com.project
[INFO] Parameter: artifactId, Value: java-module-project
[INFO] Parameter: packageName, Value: com.project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/sbhome/java-module-project
[INFO] ---------------------------------------------------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------------------------------------------------------
[INFO] Total time:  6.667 s
[INFO] Finished at: 2023-02-07T22:46:00-08:00
[INFO] ---------------------------------------------------------------------------------------------------------------------
Terminal:: $ 
After the maven command execution is complete it will create the project folder java-module-project. Next move inside the folder (cd java-module-project)
Console : Command
cd java-module-project

Create Eclipse Project

Console : Command
mvn eclipse:eclipse
Console : Output
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------------------------< com.project:java-module-project >-----------------------------------------
[INFO] Building java-module-project 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] -------------------------------------------------------[ jar ]-------------------------------------------------------
[INFO] 
[INFO] --- eclipse:2.10:eclipse (default-cli) @ java-module-project ---
[WARNING] The POM for org.apache.maven.wagon:wagon-http-lightweight:jar:1.0-beta-6 is invalid, transitive dependencies (if any) will not be available, enable verbose output (-X) for more details
[WARNING] The POM for org.apache.maven.wagon:wagon-http:jar:1.0-beta-6 is invalid, transitive dependencies (if any) will not be available, enable verbose output (-X) for more details
[WARNING] The POM for org.apache.maven.wagon:wagon-webdav-jackrabbit:jar:1.0-beta-6 is invalid, transitive dependencies (if any) will not be available, enable verbose output (-X) for more details
[WARNING] The POM for org.apache.maven.wagon:wagon-http-lightweight:jar:1.0-beta-2 is invalid, transitive dependencies (if any) will not be available, enable verbose output (-X) for more details
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "java-module-project" to /Users/sbhome/java-module-project.
[INFO] 
[INFO] ---------------------------------------------------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------------------------------------------------------------------
[INFO] Total time:  0.494 s
[INFO] Finished at: 2023-02-07T22:51:03-08:00
[INFO] ---------------------------------------------------------------------------------------------------------------------

Import Project in Eclipse



Project in Eclipse




POM file


pom.xml

<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.project</groupId>
  <artifactId>java-module-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>java-module-project</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Build Project

Console : Command
mvn install
Console : Output
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.project.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 s - in com.project.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- jar:3.2.0:jar (default-jar) @ java-module-project ---
[INFO] Building jar: java-module-project-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- install:3.0.0-M1:install (default-install) @ java-module-project ---
[INFO] 
[INFO] ---------------------------------------------------------------------------------------------------------------------
[INFO] BUILD SUCCESS

This article shows how to install Maven (3.6.3) on macOS Monterey(version 12.2.1) with M1 processor.

Download Maven

First we need to download the Maven, for that please visit the following official maven download https://maven.apache.org/download.cgi


Install Maven

Go to the download location
Normally /Users/${username}/Downloads and execute the following command

Terminal
tar -xvzf apache-maven-4.0.0-alpha-3-bin.tar.gz
x apache-maven-4.0.0-alpha-3/bin/mvn.cmd
x apache-maven-4.0.0-alpha-3/bin/mvn
x apache-maven-4.0.0-alpha-3/README.txt
x apache-maven-4.0.0-alpha-3/LICENSE
x apache-maven-4.0.0-alpha-3/NOTICE
x apache-maven-4.0.0-alpha-3/lib/
x apache-maven-4.0.0-alpha-3/lib/aopalliance.license
x apache-maven-4.0.0-alpha-3/lib/commons-cli.license
x apache-maven-4.0.0-alpha-3/lib/commons-codec.license
x apache-maven-4.0.0-alpha-3/lib/commons-lang3.license
x apache-maven-4.0.0-alpha-3/lib/failureaccess.license
x apache-maven-4.0.0-alpha-3/lib/guava.license
x apache-maven-4.0.0-alpha-3/lib/guice.license
x apache-maven-4.0.0-alpha-3/lib/httpclient.license
x apache-maven-4.0.0-alpha-3/lib/httpcore.license
It will create a folder apache-maven-4.0.0-alpha-3 which will contain unzipped contains from the downloaded file. Now Move the folder apache-maven-4.0.0-alpha-3 to final destination. In my case i moved to a folder DevRuntime which contains other runtimes for my development.
~/.zshenv
export MAVEN_HOME=~/DevRuntime/apache-maven-4.0.0-alpha-3 export PATH=$PATH:$MAVEN_HOME/bin
Now source the zshenv file
Terminal
source ~/.zshenv

Install Maven

Now Check that Maven is working
  
Terminal
mvn --verison
Apache Maven 4.0.0-alpha-3 (2ccf57baa5191468f9911fe85fd99672ac3bacb9)
Maven home: /Users/SiddB/DevRuntime/apache-maven-4.0.0-alpha-3
Java version: 18.0.1.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-18.0.1.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "12.2.1", arch: "aarch64", family: "mac"

How to skip test in Maven

Maven provides a way to only build the application code and skip executing test cases
Enter the following command to build the application code but don't execute test cases.
Skip executing tests
mvn install -DskipTests

References

How to download source for maven artifacts

Maven builds a project using its project object model (POM) and a set of plugins. Dependencies used by the project are mentioned in the pom.xml file. We can also get the dependencies artifact from maven along with source codes.

In the following section, we will cover two use cases
1) How to generate source code for third-party artifacts that my project is using
2) How to generate source code for my project
Download sources for third party artifacts
Executing the following command from the terminal will download the sources for all the artifacts for the project and will attach it to Eclipse IDE.
mvn eclipse:eclipse -DdownloadSources=true
Executing the following command from the terminal will download the sources for all the artifacts only, attaching the sources in the IDE with the binaries needed to be done seperately..
mvn dependency:sources -Dsilent=true
Generate source code for project
This command will generate the sources jar under the target folder for the project.
mvn source:jar install