Introduction to Maven

We may run an application on port 8080 in this exercise, so make sure the Jenkins service is stopped. (3:45 in the YouTube video)

Here’s a compliment to Lab 4. While Gradle is the new kid on the block, the majority of enterprise Java projects in production today use Maven.

My apologies, but this is just going to be a lot of code snippets for you to type.

Clone from GitHub

Create a folder under C:\workspace\labs\ named maven and in that directory clone the rps-maven GitHub repo:

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven
$ git clone https://github.com/cameronmcnz/rps-maven.git

You may need to then move into the MyProject directory

cd rps*

Compile Java Code with javac

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven
$ mvn compile

Clean the build directory code with maven.

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven
$ mvn clean

Run the unit tests

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven
$ mvn test
Build vs Merge with Maven Gradle and Git
Just because it will merge doesn't mean it will compile.

Package the application as an executable JAR file

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven
$ mvn package

If that gives you problems, just use the clean and install commands together.

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven
$ mvn clean install

Run the Java JAR file

In the target folder you should see a JAR file. Run it! Just make sure you’re in the target folder when you run this command.

Also, this runs on port 8080, so you may need to go to Windows Services and stop Jenkins. (3:45 in the YouTube video)

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven/target
$ java -jar spock-lizard-1.0.jar

When the app runs, it should be available at: http://localhost:8080 and http://localhost:8080/increasewins

CTRL+C in the BASH shell or command prompt kills the program.

Checkout and build the failure branch

When you try and build the failure branch you’ll have tests that fail.

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven/
$ git add .
$ git commit -m "done"
$ git branch -a
$ git checkout failure
$ mvn clean install

Checkout and build the broken branch

When you try to build the broken branch, you’ll have a compiler error.

wasadmin@CLASSPC MINGW64 /c/Workspace/labs/maven/rps-maven/
$ git add .
$ git commit -m "done"
$ git branch -a
$ git checkout broken
$ mvn clean install
FindBugs, PMD and CheckStyle

If you really want to go crazy, run this command:

 mvn compile checkstyle:checkstyle findbugs:findbugs pmd:pmd 

Now check out the Maven cheat sheet to see all of the various Maven commands that are at your disposal.

Maven Cheat Sheet
Maven Cheat Sheet.