Maven With Software Collections

I’ve been interested in the intersection of Ansible and Java development.  To test this out, I want to build a “Hello World” maven App and use Ansible to drive the process to build, test, and deploy it.  I’m going to use the Software Collections way of installing and running Maven to build a simple Tomcat Web Application as the basis.

In a past article I enabled software collections:  That is still valid and necessary.

Install the software collection

 sudo yum install rh-maven35

Enable the rh-maven35 software collection

scl enable rh-maven35 bash
[ayoung@ayoung ~]$ cd ~/devel/
[ayoung@ayoung devel]$ mkdir ghoul
[ayoung@ayoung devel]$ cd ghoul/

Then create a new WebApp with

 mvn archetype:generate -DgroupId=net.younglogic.apps -DartifactId=GhoulWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


(lots of output ending with)

[INFO] project created from Old (1.x) Archetype in dir: /home/ayoung/devel/ghoul/GhoulWebApp
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.751 s
[INFO] Finished at: 2018-08-24T21:18:44-04:00
[INFO] Final Memory: 15M/395M
[INFO] ------------------------------------------------------------------------
[ayoung@ayoung ghoul]$

Do some basic git setup.

cd GhoulWebApp/
[ayoung@ayoung GhoulWebApp]$ git init .
Initialized empty Git repository in /home/ayoung/devel/ghoul/GhoulWebApp/.git/
[ayoung@ayoung GhoulWebApp]$ ls
pom.xml src
[ayoung@ayoung GhoulWebApp]$ git add pom.xml src/
[ayoung@ayoung GhoulWebApp]$ git commit -m "initial project generation"
[master (root-commit) 783f3c1] initial project generation
3 files changed, 33 insertions(+)
create mode 100644 pom.xml
create mode 100644 src/main/webapp/WEB-INF/web.xml
create mode 100644 src/main/webapp/index.jsp

Compile with

mvn package

lots of output ending with

[INFO] Processing war project
[INFO] Copying webapp resources [/home/ayoung/devel/ghoul/GhoulWebApp/src/main/webapp]
[INFO] Webapp assembled in [17 msecs]
[INFO] Building war: /home/ayoung/devel/ghoul/GhoulWebApp/target/GhoulWebApp.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.426 s
[INFO] Finished at: 2018-08-24T21:23:37-04:00
[INFO] Final Memory: 14M/376M
[INFO] ------------------------------------------------------------------------

Run with


mvn clean install tomcat:run

lots of output ending with

[INFO] Running war on http://localhost:8080/GhoulWebApp
[INFO] Creating Tomcat server configuration at /home/ayoung/devel/ghoul/GhoulWebApp/target/tomcat
Aug 24, 2018 9:37:06 PM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Aug 24, 2018 9:37:06 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
Aug 24, 2018 9:37:06 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Aug 24, 2018 9:37:06 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

Check in a web browser:

http://localhost:8080/GhoulWebApp/And Stack trace with


org.apache.jasper.JasperException: Unable to compile class for JSP: 
An error occurred at line: 1 in the generated java file
The type cannot be resolved. It is indirectly referenced from required .class files

Known error, need to move to Tomcat 8.  Add this to the pom.xml

diff --git a/pom.xml b/pom.xml
index 4f73c5c..397207f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,6 +16,16 @@
+    <plugins>
+      <plugin>
+       <groupId>org.apache.tomcat.maven</groupId>
+         <artifactId>tomcat7-maven-plugin</artifactId>
+         <version>2.1</version>
+         <configuration>
+           <path>/</path>
+         </configuration>
+      </plugin> 
+    </plugins>

And run with


 mvn clean install tomcat7:run

Lots of output ending with

[INFO] Creating Tomcat server configuration at /home/ayoung/devel/ghoul/GhoulWebApp/target/tomcat
[INFO] create webapp with contextPath: 
Aug 24, 2018 9:52:31 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 24, 2018 9:52:31 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Aug 24, 2018 9:52:31 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Aug 24, 2018 9:52:32 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]


Look in a web browser:

Hello World!

OK,  lets check this in to git:

[ayoung@ayoung GhoulWebApp]$ git status
# On branch master
# Changes not staged for commit:
# (use "git add &lt;file&gt;..." to update what will be committed)
# (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
# modified: pom.xml
# Untracked files:
# (use "git add &lt;file&gt;..." to include in what will be committed)
# target/
no changes added to commit (use "git add" and/or "git commit -a")
[ayoung@ayoung GhoulWebApp]$ git add pom.xml
[ayoung@ayoung GhoulWebApp]$ git commit -m "Move to tomcat7 or later"
[master 1e82829] Move to tomcat7 or later
1 file changed, 10 insertions(+)

And we have a sample app to use with Ansible.






Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.