As requested by some FreenixSecurity users present at Taranto’s Linux Day
(Pietro Acclavio library, October 28, 2017), today we’ll talking about Apache Maven and Java.
Apache Maven is a tool for managing Java-based projects that support developers in compilation processes, resolving the dependencies with an automatic download of project libraries, automating the creation of a simple documentation site for each projects, tests, distributing and much more.
Briefly, Maven works by processing the pom.xml file contained in all projects.
This file, acronym for ‘Project Object Model‘ describe and manage the projects and its structure, the various dependencies, and also the compilation processes with a possible documentation building.
In this how to, we’ll see how to use Maven to structure a simple Java project.
Let’s try to find out if Apache Maven is available in the repo, opening the terminal and typing:
apt-cache search maven
Just in case that wasn’t we can proceed with the installation typing:
sudo apt-get install maven
To view the version:
Typing this, on my Debian Wheezy appears:
Apache Maven 3.0.4 Maven home: /usr/share/maven Java version: 1.7.0_80, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-7-oracle/jre Default locale: it_IT, platform encoding: UTF-8 OS name: "linux", version: "3.2.0-4-686-pae", arch: "i386", family: "unix" vincenzo@vincenzo:~/maven_exercise/freenix_maven/freenix_maven-app$
Now, let’s create the architecture of the project which we call: freenix_maven-app:
mvn archetype:generate -DgroupId=com.freenix_maven.app -DartifactId=freenix_maven-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Accessing the directory we can see our Hello World created in src/main/java/com/freenix_maven/app/App.java.
Now we enter the project folder and give this command in this way:
cd freenix_maven-app mvn package
Now, finally, we can test our project typing:
java -cp target/freenix_maven-app-1.0-SNAPSHOT.jar com.freenix_maven.app.App Hello World!
Now that’s all ready, we can create the site calling mvn with the ‘site’ parameter:
This site is generated into the directory target/site that we access in order to launch the index file with the browser:
cd freenix_maven-app/target/site iceweasel index.html
P.s.: I personally use Iceweasel, you can use what you like