How to undeploy a .war with Jenkins? - maven

we are working on our Selenium Grid Setup.
We did it like this:
We got 2x Jenkins Jobs and 2x Tomcats.
We are using Maven!
First Jenkins Job deploys the application (to test) on a tomcat. This job got a post action where the second job is triggered. (of course with ignored testing)
Second Jenkins Job tests the application deployed on the first tomcat and if its successful it will deploy the application on the second and final Tomcat.
Now we need to undeploy the application on the first Tomcat if its deployed on the second one.
Is there any option to integrate this in the second Jenkins Job?
It would be nice if you could give me some advice.
Thank you
-T

Typically, the way to deploy and undeploy on Tomcat in an automated fashion is to use the Maven Tomcat plugin. Disregard the fact that it seems to be only for Tomcat version 7 - it will work the same for 7+.
Behind the scenes, this plugin utilizes the Tomcat manager REST API.
Your Jenkins commands would probably go like:
mvn tomcat7:deploy on first server
If the deploy and startup is successful, you'll get a verifiable response that looks like: "OK - Deployed application at context path /foo" (and likely the relative error code)
You could also write a curl and parse command
for the manager command at http://localhost:8080/manager/text/list
to check if the application deployed as a sanity check
If it deployed
Do a mvn tomcat7:deploy on the second server and
Do a mvn tomcat7:undeploy on
the first server.
Just for all of the information to be here, the basic maven configuration for this plugin is:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/myAppContextPath</path>
<username>admin</username>
<password>admin</password>
</configuration>
</plugin>

Related

Execute application using mvn clean install

I am working on a simple spring boot application (created from spring initializr contains web mega-dependency).
I added also Cucumber dependencies for acceptance test.
When I run mvn clean install it runs directly the cucumber test, but me I want to run the app before tests (And why not stop the app after the tests terminate).
I tried maven exec plugin but it does not work:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>org.dhappy.test.NeoTraverse</mainClass>
</configuration>
</plugin>
Skipping tests
If there is a need to skip tests and just compile and package the application, you can set the skipTests property as follows:
mvn install -DskipTests
Application execution
If you want to run the application, you can use the exec plugin. There are two flavours ("goals" really): one runs the package application and the other runs a Java class. If you know the class name, you can run that plugin as follows:
mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse"
Integration testing
Finally, if you want to run the application first before you can run the tests, you are actually not doing unit testing. Your tests in this case are perhaps integration and not unit tests. The actual details would vary depending on whether your application has complex needs such as a database and a web server.
Integration testing happens at a different "phase" in maven lifecycle.
In short, you need to rename your Java test classes something other than *Test.java so that maven surefire plugin ignores your tests. You then need to include maven-failsafe-plugin so that integration testing happens.
You want to perform these steps:
Start the application
Run a set of tests
Stop the application
Starting and stopping the application is one thing and could be done in the <phase>pre-integration-test</phase> and <phase>post-integration-test</phase>. Integration testing is done in the <phase>integration-test</phase>
You want to read and understand https://maven.apache.org/surefire/maven-failsafe-plugin/usage.html. Specifically, look at the Using jetty and maven-failsafe-plugin example.

Make maven run one task before another when running a single plugin

I've got a project set up using the Maven Cargo plugin to launch Tomcat with my webapp deployed in it, along with some other webapps that are needed for support. This works great. Unfortunately, when I run "mvn cargo:run" it doesn't do a build first, but instead just actually starts Tomcat running the code the last time I did do a build.
Previously I used the tomcat7 plugin instead, and that did do a build first and always ran the current version of the source code. As such, I could change my code and run "mvn tomcat7:run" and know that the code changes had been built and were running.
I can't find any way with the Cargo plugin to make it do this, but is there some way with Maven to make it at least run the Package phase when I run a specific plugin so that it will build the WAR file correctly first?
The Tomcat plugin automatically invokes the compile phase prior to executing itself. The Cargo plugin won't do that. In order to compile your code before executing the plugin, you need to run
mvn clean compile cargo:run
If you want to start and stop the container automatically before and after your integration tests, you can also bind cargo:start and cargo:stop to Maven's lifecycle phases. See Automatically executing and stopping the container when running mvn install for details.
Here is a full example how to integrate the start via Cargo in the usual build. https://github.com/khmarbaise/maui/tree/master/src/main/resources/it-example-container. You can start the integration tests via mvn -Prun-its clean verify which might be better
A completely different approach would be to use the exec-maven-plugin to execute multiple goals with one command:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>clean</argument>
<argument>compile</argument>
<argument>cargo:run</argument>
</arguments>
</configuration>
</plugin>
<!-- more plugins... -->
</plugins>
</build>
This way, you would only have to call
mvn exec:exec
to clean, compile and run your application.

Deploying a Maven Spring Boot project on a server offline

I have a situation in hand where I have no internet connectivity on my client's server (because it is an intra-net server) and I have to deploy my Spring Boot application on it which uses Maven build. The thing is, I am not allowed to connect the server to the internet due to security reasons but I am able to access internet on my development machine. So I have two questions:
How do I get all the dependencies on the server in question? Can I somehow copy the downloaded files from my development machine to the server? If so, how?
Do I need to setup Apache Tomcat or should I just run the war file from the command line? What are the best practices?
It's better to make a jar file instead of making a war file. That should help you to run without Tomcat since it embeds a web server.
Dependencies are packed with the jar when you do a Maven build. You need to have an internet connection to build it. Copy the jar to the production environment and run.
Here is striaght forward example to deploy spring boot application on tomcat server
Follow the example and add one more tomcat6/tomcat7 plugin
<!-- Tomcat 6 plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
<url>${serverUrl}:${port}/manager</url>
<path>/${contextpath}</path>
<username>${username}</username>
<password>${password}</password>
</configuration>
</plugin>
plugin properties as follows
<properties>
<!-- Server Details -->
<serverUrl>http://kpServer</serverUrl>
<port>8080</port>
<contextpath>kp-cxf</contextpath>
<username>admin</username>
<password></password>
<!-- Skip test execution during default steps -->
<skipTests>true</skipTests>
</properties>
And you can use mvn tomcat6:redeploy command to deploy to your server(in shore maven deownloads plugins/dependencies from internet to your development server, when you run mvn tomcat6:deploy/package it creates war by taking jars from you local maven directory)

How to see changes without building maven project?

I have simple java web application. web application has some js, css, html files. when I change js files, i am not able to view new changes in browser. For new changes i have to perform "mvn clean install" command then only i am able to see new changes. So Is there any way to see changes without performing this command ?
thanks.
it's not recommended to perform "mvn clean install" unless you want to use your code into another project.
That's what I do when I want to publish code change into a webserver for debug purposes
1/ I configure tomcat7-maven-plugin into the parent pom project:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
2/ then
mvn tomcat7:run
will launch an embded tomcat7 server with your resources deployed.
to publish your code to a remote tomcat, you can use
tomcat7:deploy
after having configured tomcat7 plugin correctly
alternatively, import your maven project as an eclipse project (plugin name: m2e) then right click on the projet, debug on server will launch the eclipse configured tomcat.
whenever you save a resource, it will be automagically deployed on the eclipse-managed tomcat.

Remote Deploy to WebSphere 8.5 using maven

I would like to control a remote deployment of a maven generated EAR over to an existing WAS instance not running on the build server.
Ideally, I would like to do this within Maven so that I can remote deploy in say the integration-test phase then proceed to run some JMeters in the verify phase. I guess this is pretty standard.
I have looked around and am unable to find a sensible way to do this in WAS 8.5.
There are a few posts:
Remote Deployment to WAS 6.1
websphere7am-maven-plugin
Cargo
and others around the web, including IBM. None seem to offer a way to achieve a remote deploy to WAS 8.5
Does anybody have a solution?
EDIT 1:
Further confirmation from IBM that no official maven solution exists can be found here:
WAS 8.5 - Using Ant to automate tasks
AFAIK there is no Maven plugin for full-fledged WAS 8.5, only for WAS Liberty Profile. But that one does not support deployment to remote server.
Remote deployment can be done using WsAdmin Ant Task & Maven AntRun Plugin
<plugin>
<groupId>com.orctom.mojo</groupId>
<artifactId>was-maven-plugin</artifactId>e
<version>1.0.8</version>
<executions>
<execution>
<id>deploy</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<wasHome>${env.WAS_HOME}</wasHome>
<applicationName>${project.build.finalName}</applicationName>
<host>${local or remote address}</host>
<server>server01</server>
<node>node01</node>
<virtualHost>default_host</virtualHost>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
From https://github.com/orctom/was-maven-plugin
Updated on 5/29/2014
Developer of this plugin states on github, "1.0.1 and 1.0.2 is not working, please don't use them!", so I've updated this answer to show version 1.0.3.
Updated on 1/27/2015
Updated to '1.0.8'.
You will soon be able to automatically deploy to WebSphere remotely using Jenkins. You can create a maven project and have Jenkins automatically deploy to WebSphere with the plugin listed below. If you're interested, follow it since I'll be taking feature requests for a limited time.
https://wiki.jenkins-ci.org/display/JENKINS/WebSphere+Deployer+Plugin
Enjoy!
we're using was6-maven-plugin to perform both local and remote deployments to WAS 7. Internally this plugin uses ant tasks from wsadmin so I guess it would also work with WAS 8.5.

Resources