How to make Jenkins job fail in case of java exception? - java

I have Jenkins job. I run maven java-executor plugin as follows:
compile exec:java
here is the pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.proj.utills</groupId>
<artifactId>db-upgrade</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.proj.db.RunMe</mainClass>
<arguments>
<argument>
${TARGET_ENV}
</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
I would like that Jenkins job fail when I got exception in Java code. For now it success even in case of exception. How can I do it?
Thanks

You can use the log parser plugin
EDIT: Excerpt from the plugin's documentation
Job Configuration
Go to menu: Jenkins -> job name -> Configure
Go to section : Post-build Actions
Check the "Console output (build log) parsing" checkbox.
"Mark build Unstable on Warning" option: check to have parsed warnings mark the build 'unstable'.
"Mark build Failed on Error" option : check to have parsed errors mark the build 'failed'.
"Select Parsing Rules" : select the set of rules to use for parsing the logs of this job's builds
(Notice that this list is derived from the global configuration )

I use the Log Parser Plugin for cases like this. just configure what you are looking for. In your case it might be as simple as looking for 'Exception'. I used to parse the output of shell scripts, to find sql errors (but still ignoring some that I don't care about).

You may try the Jenkins Text Finder plugin, downgrade the BUILD status from successful to UNSTABLE/FAIL when the regular expression setting is met. More detail refer to the link above.

I got exactly the same problem and did not like this parsing log solution.
For me a -B (to enable the batch mode) did the work nicely
build:
- mvn -B deploy

Related

Maven plugin CLI parameter overwriting <configuration> in pom.xml

How does/should Maven plugins behave with regards to the order in which they process configuration options? I would expect that properties passed via CLI overwrite those defined in a <configuration> block in pom.xml.
Here's an example.
pom.xml
<plugin>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3</version>
<executions>
...
</executions>
<configuration>
<url>foo.com</url>
</configuration>
</plugin>
CLI
mvn group:artifact:1.2.3:doit -Dmymojo.url=bar.com
I am currently debugging a plugin (not mine) that gives precedence to the url value defined in the POM rather than the one passed on the CLI. Is that how mojos are supposed to behave i.e. a Maven feature rather than a plugin bug? I didn't find anything mentioned in the ref guide.
As per https://issues.apache.org/jira/browse/MNG-4979 this works as designed. I find it counter-intuitive and don't find the reasons given in MNG-4979 convincing.
If your setup allows to modify the pom.xml you can work around this behavior as suggested by JF Meier (and the issue above).
<properties>
<mymojo.url>foo.bar</mymojo.url>
</properties>
<plugin>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3</version>
<executions>
...
</executions>
<configuration>
<url>${mymojo.url}</url>
</configuration>
</plugin>
Through the command line, you set a property url. This would override an entry <url>foo.com</url> in the <properties> section of the POM.
Many plugins allow to set configuration entries through properties, but these properties do not automatically have the same name. In the documentation, this is usually called user property. To see examples, look e.g. at
https://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html

How to set project version in maven at build time?

I am trying to implement semantic versioning in our project. I tested maven semver plugin but that didn't help me so please don't ask me why. I finally ended up using maven groovy. It works like a charm, however, when I install or deploy the maven project the version in repository is the variable name.
This is despite the fact that all the artefacts and jar files are packaged with correct version.
So please look at my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytest.test</groupId>
<artifactId>test-tag</artifactId>
<version>${revision}</version>
<description>Test</description>
<properties>
<ChangeType>TO_BE_SET</ChangeType>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<providerSelection>2.0</providerSelection>
<properties>
<script>git describe --abbrev=0 --tags</script>
</properties>
<source>
def tagIt = 'git tag -a vXXXX -m "Auto tagged"'
def changeType = project.properties.ChangeType
def command = project.properties.script
def process = command.execute()
process.waitFor()
def describe = process.in.text.trim()
println "Setting revision to: " + describe
if(!describe.startsWith("v")) {
describe = "1.0.1"
} else {
describe = describe.substring(1)
}
project.properties.setProperty('revision', describe)
</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-testResources</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
the version is ${revision} a variable name that is being set in groovy script. What groovy code does is getting the last tag from GIT and and then set it to the property 'revision'.
The final jar file has the correct version extracted but when installed into repository, the folder name and jar name are like:
m2\repository\com\mytest\test\test-tag\${revision}\test-tag-${revision}.jar
I tried to default 'revision' to a value using:
<properties>
<revision>1.0.1</revision>
</properties>
but then groovy code setting the value has no effect.
I also tried different phase for the maven groovy plugin, no luck. Have I missed anything? Can anyone please help me on this?
I'd like to mention that as vatbub and StefanHeimberg mentioned I can use versions:set to set the version but this requires me to do an extra commit to GIT which I am trying to avoid, wondering if I can achieve this by writing a maven plugin instead?
With Maven you can set the version at build time with
mvn versions:set -DnewVersion=${bamboo.inject.version}
as #vatbub already commented in your question.
In addition to this i wrote a Shell script that can be used in build pipeline to generate the version according to the maven project version and add the build number from the build server.
https://gist.github.com/StefanHeimberg/c19d7665e8df087845c036fe8b88c4f2
The Script reads the maven project version, add a the build number and writes a text file with all the new numbers that can be used.
The next step is to inject this text file in the Build Pipeline and call the versions plugin as stated above
pom.xml:
something like
<project>
<groupId>ch.stefanheimberg.example</groupId>
<artifactId>your-awesome-app</artifactId>
<version>5.1.2-SNAPSHOT</version>
</project>
or
<project>
<groupId>ch.stefanheimberg.example</groupId>
<artifactId>your-awesome-app</artifactId>
<version>5.1-SNAPSHOT</version>
</project>
Step 1:
./generate_version_txt.sh ${bamboo.buildNumber}
Step 2:
Inject generated version.txt in the build system that all the properties can be used in all tasks / plugins, etc...
In my case Bamboo CI ready the version.txt file and declares the content of the file as environment variables under the bamboo.inject. prefix.
For example ${bamboo.inject.long_version}
Step 3:
Update Maven Project version
mvn versions:set -DnewVersion=${bamboo.inject.version}
Step 4:
Run Maven Build
mvn clean verify
Step 5:
Run Docker build
for example use it also as docker tag version. etc...
docker build --build-arg version=${bamboo.inject.version} --tag your-awesome-app:${bamboo.inject.version} .
Example Dockerfile:
FROM jboss/wildlfy
ARG version
ADD target/your-awesome-app-${version}.war /opt/jboss/wildfly/standalone/deployments/
I know that can be a problem / not possible in your case with the groovy script. but perhaps it is an other view at your problem. and possibly also another solution for it.
(sorry for my english. but i hope it is understandable what i mean)
I ran into a similar problem and ended up using the maven flatten plugin to ensure that all variables are removed from the POMs before being deployed. This remove all references to the string ${revision} and replace by the actual value at build time, without interfering with the original POMs.

Problems converting Dynamic Web Project to Maven

I'm using Eclipse Luna with Websphere Application Sever 8.5.
When I run a dynamic web project using the Websphere Application Sever 8.5 it runs fine.
However, when I convert a dynamic web project to a maven project, i'm getting around 53 errors starting with Missing artifact com.ibm.websphere e.g Missing artifact com.ibm.websphere.xml:xmlapi:jar:1.0.0' all located in my pom.xml.
I am also getting this error:
The container 'Maven Dependencies' references non existing library
C:\Users\Brian242\.m2\repository\com\ibm\websphere\ws\com.ibm.ws.wccm\1.0.0\com.ibm.ws.wccm-1.0.0.jar'
Not sure if that's part of the problem.
I've looked everywhere for a solution but haven't found anything. So I have decided to ask for some help. Does anyone know how to solve the above problem/s?
Thank you,
Brian
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TrialProj</groupId>
<artifactId>TrialProj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.ibm.tools.target</groupId>
<artifactId>was</artifactId>
<version>8.5.5</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
you created M2_REPO environment variable?
right click on the project root
Build Path-> Configure Puild Path ...
Click button 'Add Variable'
Click 'Set Variable ...'
Click 'New ...'
name: M2_REPO
Path: / path_to_repository_the_maven /
Click Ok
Recompile
If you are using eclipse make sure you add suport for eclipse web tools using:
mvn eclipse:eclipse -Dwtpversion=2.0
(2.0 or your required version) And afterwards recompile maven project:
mvn compile

Maven PMD plug-in not generating a report with 'mvn site' command or 'pmd:pmd'

I am reading an interesting tutorial here: http://www.avajava.com/tutorials/lessons/how-do-i-generate-pmd-and-cpd-reports-for-a-site.html?page=1
This tutorial shows how to use Maven to run the open-source static-analysis tool, PMD, and to see the generated output on a Maven created website. Maven can easily create websites with mvn site command, but this tutorial shows how to use PMD for more helpful metrics on the source code.
The instructions were followed to the best of my ability. Here is my pom.xml file that came from reading the tutorial:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.name.bookstore</groupId>
<artifactId>bookstore</artifactId>
<packaging>jar</packaging>
<version>1</version>
<name>bookstore</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<!-- start for deploying using webdav -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav</artifactId>
<version>1.0-beta-2</version>
</extension>
</extensions>
</build>
<distributionManagement>
<!-- start -location where site is deployed -->
<site>
<id>site.deployments</id>
<name>Site deployments</name>
<url>dav:localhost/${basedir}</url>
</site>
</distributionManagement>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
</plugin>
</plugins>
</reporting>
</project>
When I run the command: mvn clean site I get a website built by Maven with a bunch of different pages but none of them show anything in regards to PMD. What am I missing here? Why am I not seeing anything in regards to PMD in the generated website?
Also, when I run mvn pmd:pmd there is a successful build but I do not obtain any helpful PMD metrics. I even coded in some unused variables and methods in one of my Java source files as illustrated in the above linked tutorial and there is no helpful output.
The mvn pmd:pmd command does appear to create some files though. A couple are files of rules for the engine it looks like and the others are empty. Please see the screen-shots of this below:
Figure 1: Files created by pmd:pmd command
Figure 2: Empty pmd file - even though there are obvious errors in Java source file
Does anyone out there know what is up? Why is PMD not working with Maven for me?
Thanks for reading this.
Also, from what I have read around the Internet on PMD's website and Maven's website there should be some information in the "Project Reports" section. There is no data here though from PMD. Please see the below screen-shot.
Figure 3: No PMD Data found in Project Reports
Regards
UPDATE
When I change the PMD section of the pom.xml file to the below snippet I obtain some CPD results via PMD but still nothing from PMD on code bugs. I even coded in an NullPointerException and PMD said nothing even when issuing the mvn pmd:check command.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>1</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
</plugin>
In the snippet I changed the sourceEncoding tag to be utf-8 because everything I see in regards to this is utf-8. I also changed the minimumTokens value to 1 to try to get more output from this plug-in. I also put this snippet in the <build> section to try and get results but still nothing... :/
Thanks for studying this...
The maven-pmd-plugin by default skips nowadays empty reports (property skipEmptyReport). You'll need to set this to false, to get in your site always a PMD/CPD report:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<configuration>
<skipEmptyReport>false</skipEmptyReport>
</configuration>
</plugin>
</plugins>
</reporting>
This applies for both PMD and CPD. I assume, this is your problem, as in Figure 2, you show, there are no PMD violations detected (pmd.xml file is empty).
The property minimumTokens configures CPD and defines how long a code snipped at a minimum must be to be declared as a duplicate. The lower the number, the more duplicates are detected, but the duplicates can also be much shorter and therefore maybe more often false positives.
Without further configuring maven-pmd-plugin it uses by default these three PMD rulesets: java-basic, java-imports, java-unusedcode. See also property rulesets. If you want to detect specific problems, then you'll need to enable these rules. See also How to make a ruleset.

Maven plugin not running where I want it to

I have a maven project that I want to load properties from a file when things happen. I've got the codehaus properties-maven-plugin and I need it to run automagically.
If I run mvn test or mvn compile, the task plugin load the properties file just fine.
I see this in the output:
[INFO] --- properties-maven-plugin:1.0-alpha-2:read-project-properties (default)...---
When I run mvn flyway:init, say, I do not see it run and the properties do not get loaded.
Here is the pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.foo</groupId>
<artifactId>workflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Foo Framework</name>
<description>Foo framework</description>
<properties>
<postgre.version>9.1-901.jdbc4</postgre.version>
<flyway.version>2.2.1</flyway.version>
</properties>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgre.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<url>${url}</url>
<user>${user}</user>
<password>${pass}</password>
<locations>
<location>classpath:sql/pgsql</location>
</locations>
</configuration>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgre.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/db.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Can anyone suggest what I can do, or some documentation I can read to make this work? I have read plenty of maven documentation and cannot seem to understand it well enough to make this happen. I thought what I have here would make it run during the initialize phase, but evidently not...
You are close. There is a difference between running Maven using a phase, and using a goal. Start with a quick review of the Maven lifecycle documentation.
When you run mvn compile, Maven runs all goals bound to the compile phase, plus anything bound to earlier phases. You have bound the properties:read-project-properties goal to the initialize phase. initialize is one of the first phases, so read-project-properties executes when compile, or test, or any of the later phases are used in the command.
When you run mvn flyway:init, you are only running a single goal, not the entire lifecycle. Thus, the only thing that runs with that command is the flyway-maven-plugin's init goal, nothing else.
If you want those two goals to run together, you could try binding the flyway:init goal to the initialize phase (if that is not the default; I could not tell from reading the plugin's documentation). Then, mvn initialize will run both.
If you don't want to bind flyway:init to a phase, then you may run both goals explicitly with mvn flyway:init initialize.

Categories