Bootstrap FreeKB - Maven - Creating your first Maven app on Linux
Maven - Creating your first Maven app on Linux

Updated:   |  Maven articles

This assumes you have install the Maven mvn CLI. If not, check out my article FreeKB - Maven - Install the mvn CLI on Linux.

Let's create a temp directory (on a Linux system)

mkdir /tmp/maven

 

And move into the temp directory.

cd /tmp/maven

 

And issue the following command to create your first Maven app.

mvn \
archetype:generate \
-DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false

 

The following should be displayed at the end of the output.

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: packageInPathFormat, Value: com/mycompany/app
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Project created from Archetype in dir: /tmp/maven/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.538 s
[INFO] Finished at: 2024-08-22T20:25:18-05:00
[INFO] ------------------------------------------------------------------------

 

And there should be a directory named my-app.

[john.doe@localhost maven]$ ls -l
total 0
drwxr-xr-x. 3 john.doe admins 32 Aug 22 20:25 my-app

 

And the my-app directory should have the following files and subdirectories.

├── pom.xml
├── src -> main -> java -> com -> mycompany -> app -> App.java
├── src -> test -> java -> com -> mycompany -> app -> AppTest.java

 

The App.java file contains a public class named App that prints Hello World.

[webproc@localhost maven]$ cat my-app/src/main/java/com/mycompany/app/App.java 
package com.mycompany.app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

 

 Let's change into the my-app directory.

cd my-app

 

And package the app.

mvn package

 

If all goes well, the following should be at the end of the output.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.706 s
[INFO] Finished at: 2024-08-22T20:39:34-05:00
[INFO] ------------------------------------------------------------------------

 

And there should now be a directory named "target" that contains a number of files and directories.

[john.doe@localhost my-app]$ ls -l target/
total 4
drwxr-xr-x. 3 john.doe admins   17 Aug 22 20:39 classes
drwxr-xr-x. 3 john.doe admins   25 Aug 22 20:39 generated-sources
drwxr-xr-x. 3 john.doe admins   30 Aug 22 20:39 generated-test-sources
drwxr-xr-x. 2 john.doe admins   28 Aug 22 20:39 maven-archiver
drwxr-xr-x. 3 john.doe admins   35 Aug 22 20:39 maven-status
-rw-r--r--. 1 john.doe admins 3071 Aug 22 20:39 my-app-1.0-SNAPSHOT.jar
drwxr-xr-x. 2 john.doe admins   85 Aug 22 20:39 surefire-reports
drwxr-xr-x. 3 john.doe admins   17 Aug 22 20:39 test-classes

 

And the "java" CLI can be used to run the JAR file which should return Hello World. Nice!

[john.doe@localhost my-app]$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Hello World!

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter faa43e in the box below so that we can be sure you are a human.