Java classes missing from jar built with maven - java

When i try to run my .jar the classes cannot be found?
Its a basic app, contains 2 custom classes and commons-lang
The .jar contains classes for commons-lang but not for my java classes.
rob#work:~/git/ProjectName/target$ java -jar uber-ProjectName-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/server/Main
Caused by: java.lang.ClassNotFoundException: com.test.server.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: com.test.server.Main. Program will exit.
This is 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.server</groupId>
<artifactId>ProjectName</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>thisisatest</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.test.server.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>uber-${artifactId}-${version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
build output ..
rob#work:~/git/ProjectName$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building thisisatest
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] Ignoring available plugin update: 2.3 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.2 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.1 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.0 as it requires Maven version 3.0
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rob/git/ProjectName/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rob/git/ProjectName/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] Surefire report directory: /home/rob/git/ProjectName/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /home/rob/git/ProjectName/target/ProjectName-1.0-SNAPSHOT.jar
[INFO] [shade:shade {execution: default}]
[INFO] Including commons-lang:commons-lang:jar:2.3 in the shaded jar.
[INFO] Replacing /home/rob/git/ProjectName/target/uber-ProjectName-1.0-SNAPSHOT.jar with /home/rob/git/ProjectName/target/ProjectName-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 06 22:23:33 IST 2014
[INFO] Final Memory: 17M/179M
[INFO] ------------------------------------------------------------------------

[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
Maven can't find any code to compile. This looks very suspicious. Perhaps your maven project is not configured like a maven project should be and it can't find the source. See the project structure diagram from maven.

[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
Maven isn't finding your sources, you may not have the directory structure correct. Take a read through http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

I encountered this issue when I had an extra space before the "main" folder. You can check your folder structure and see if it's correct.

Related

error: cannot find symbol(Heroku Discord Bot)

I'm trying to make my Discord bot to work on Heroku but I keep getting an error. The build process works fine but the bot doesn't go online. I went to Resources --> More --> View Logs and saw an error. I understood the error as some methods not getting recognized and I removed the error one. But another one would just pop up. The code and bot works on local using IntelliJ. Anyone can help me with this please?
The error/Resource logs:
2021-04-18T23:58:17.202220+00:00 app[Worker.1]: location: class Main
2021-04-18T23:58:17.226725+00:00 app[Worker.1]: src/main/java/Main.java:9: error: cannot find symbol
2021-04-18T23:58:17.226770+00:00 app[Worker.1]: jda.setActivity(Activity.listening("!help"));
2021-04-18T23:58:17.226835+00:00 app[Worker.1]: ^
2021-04-18T23:58:17.226906+00:00 app[Worker.1]: symbol: variable Activity
2021-04-18T23:58:17.226942+00:00 app[Worker.1]: location: class Main
2021-04-18T23:58:17.228071+00:00 app[Worker.1]: 5 errors
2021-04-18T23:58:17.230942+00:00 app[Worker.1]: error: compilation failed
2021-04-18T23:58:17.336252+00:00 heroku[Worker.1]: Process exited with status 1
2021-04-18T23:58:17.440163+00:00 heroku[Worker.1]: State changed from up to crashed
Build/Deploy logs:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/java
-----> Java app detected
-----> Installing JDK 15... done
-----> Installing Maven 3.6.2... done
-----> Executing Maven
$ mvn -DskipTests clean dependency:list install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< org.example:CalciteDiscordBot >--------------------
[INFO] Building CalciteDiscordBot 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # CalciteDiscordBot ---
[INFO] Deleting /tmp/build_6a1072dc/target
[INFO]
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) # CalciteDiscordBot ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # CalciteDiscordBot ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/build_6a1072dc/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # CalciteDiscordBot ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 21 source files to /tmp/build_6a1072dc/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # CalciteDiscordBot ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/build_6a1072dc/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # CalciteDiscordBot ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # CalciteDiscordBot ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # CalciteDiscordBot ---
[INFO] Building jar: /tmp/build_6a1072dc/target/CalciteDiscordBot-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) # CalciteDiscordBot ---
[INFO] Installing /tmp/build_6a1072dc/target/CalciteDiscordBot-1.0-SNAPSHOT.jar to /tmp/codon/tmp/cache/.m2/repository/org/example/CalciteDiscordBot/1.0-SNAPSHOT/CalciteDiscordBot-1.0-SNAPSHOT.jar
[INFO] Installing /tmp/build_6a1072dc/pom.xml to /tmp/codon/tmp/cache/.m2/repository/org/example/CalciteDiscordBot/1.0-SNAPSHOT/CalciteDiscordBot-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.854 s
[INFO] Finished at: 2021-04-18T23:57:57Z
[INFO] ------------------------------------------------------------------------
-----> Discovering process types
Procfile declares types -> Worker
-----> Compressing...
Done: 76.7M
-----> Launching...
Released v23
https://calcitediscordbot.herokuapp.com/ deployed to Heroku
Main.java
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I inserted token here");
jda.setActivity(Activity.listening("!help"));
jda.build();
}
}
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>org.example</groupId>
<artifactId>CalciteDiscordBot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>15</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_246</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
</project>
Procfile:
Worker: java src/main/java/Main.java
If you run locally with an IDE like IntelliJ, it will handle things like this to you, but you need add a plugin to create a jar with dependencies before deploying it.
<?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>org.example</groupId>
<artifactId>CalciteDiscordBot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>...</properties>
<dependencies>...</dependencies>
<repositories>...</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
To fix this problem, you'd have to change the pom.xml and Procfile.
Add this line to the pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.1.0</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>MAIN_CLASS_PATH</mainClass>
<name>BOT_NAME</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
Add this inside of your plugins tag in your build tag. Change MAIN_CLASS_PATH to your own main class and change BOT_NAME to your project's/bot's name.
Change the Worker for the Procfile to this:
worker: sh target/bin/BOT_NAME
Same thing here ^, change BOT_NAME to your project's/bot's name.
Most credit goes to BooleanCube's Discord. And thank you #Kaneda for the help!

Using XML snippets in Maven Polyglot

As a polyglot maven user, I have maven configuration snippet written in xml
For example
Listing 1. Using maven-jar-plugin to modify MANIFEST.MF
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
taken from http://www.ibm.com/developerworks/library/j-5things13/
How can I use it in ,say, my pom.yaml ?
straightforward translation fails
$ mvn io.takari.polyglot:polyglot-translate-plugin:translate -Dinput=maven-jar-plugin.xml -Doutput=maven-jar-plugin.yaml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-translating-snippets 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- polyglot-translate-plugin:0.1.15:translate (default-cli) # maven-translating-snippets ---
[INFO] Translating D:\Workspaces\STS-373\maven-translating-snippets\maven-jar-plugin.xml -> D:\Workspaces\STS-373\maven-translating-snippets\maven-jar-plugin.yaml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.466 s
[INFO] Finished at: 2016-04-19T10:00:29+08:00
[INFO] Final Memory: 6M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.takari.polyglot:polyglot-translate-plugin:0.1.15:translate (default-cli) on project maven-translating-snippets: Error translating maven-jar-plugin.xml -> maven-jar-plugin.yaml: Expected root element 'project' but found 'plugin' (position: START_TAG seen \t\t\t<plugin>... #1:11) -> [Help 1]
[ERROR]
I was able to migrate my pom.xml using the official doc
I wrote something about how I did it and the problems I encountered here

Maven: package org.junit does not exist

I'm new to Maven, so excuse me if this is so elemental. I have searched about this problem, and everyone seems to solve it removing the tag from the pom.xml file.
I do not have that tag there:
<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>GraphApp</groupId>
<artifactId>GraphApp</artifactId>
<version>1.0.0</version>
<name>MyApp</name>
<description>MyApp</description>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have converted an existing project with Eclipse, so the problem may come from that.
Also, I have read that the default schema for Maven is storing the code at some default folders, while I'm using src/com/romanrdgz/core/myapp and /test/com/romanrdgz/test/core. That could also be the origin of the problem.
I am seeing this error:
Maven: package org.junit does not exist
What do I need to change?
EDIT: Ok, I have included JUnit4 as dependency, and I have also changed my file folders to be like this:
Still, when I go to console and execute mvn test or mvn compile, I get the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building GraphApp 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # GraphApp -
--
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\rrrp\workspace\GraphApp\src\
main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # GraphApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.515 s
[INFO] Finished at: 2015-04-15T08:15:06+02:00
[INFO] Final Memory: 11M/309M
[INFO] ------------------------------------------------------------------------
I tried erasing bin and target folders, but still the same. Looks like it does not find the classes nor the tests.
Any help?
Add the junit dependency to the pom file in a dependencies section
<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">
.....
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>

force lifecycle prerequisite for maven plugin execution?

I use the maven-properties-plugin during the initialization phase to read in a bunch of properties from a properties file.
I also have the jetty plugin configured to set a couple of the project properties - including those read in above - as system properties to jetty.
If I run the result as
mvn initialize jetty:run-war
it works.
If I just say
mvn jetty:run-war
it fails. How can I force a goal specified on the command line to run in a lifecycle that includes the initialization phase?
Following works for me. Hope this helps.
file: pom.xml
<profiles>
<profile>
<!-- mvn -Plocal -->
<id>local</id>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run-war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
If I just say mvn jetty:run-war it fails.
That's not what I'm experiencing. If you look at the documentation of the jetty:run-war goal, you'll see that it:
Invokes the execution of the lifecycle phase package prior to executing itself.
So all the phases preceding package plus the package itself are run and, consequently, the plugins bound to them. And indeed, with the following POM:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>q2488581</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>q2488581 Maven Webapp</name>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
</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>etc/config/dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Running mvn jetty:run-war produces the following output:
$ mvn jetty:run-war
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building q2488581 - Maven Webapp
[INFO] task-segment: [jetty:run-war]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing jetty:run-war
[INFO] [properties:read-project-properties {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/q2488581/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [war:war {execution: default-war}]
[INFO] Packaging webapp
[INFO] Assembling webapp[q2488581] in [/home/pascal/Projects/stackoverflow/q2488581/target/q2488581]
[INFO] Processing war project
[INFO] Copying webapp resources[/home/pascal/Projects/stackoverflow/q2488581/src/main/webapp]
[INFO] Webapp assembled in[76 msecs]
[INFO] Building war: /home/pascal/Projects/stackoverflow/q2488581/target/q2488581.war
[INFO] [jetty:run-war {execution: default-cli}]
...
As we can see, properties:read-project-properties is invoked during the initialize phase (before process-resources to which resources:resources is bound) as expected.
In other words, I cannot reproduce your problem (or maybe you should be more specific).

How to get Cobertura to fail M2 build for low code coverage

I'm trying to configure my WAR project build to fail if the line or branch coverage is below given thresholds. I've been using the configuration provided on page 455 of the excellent book Java Power Tools, but with no success. Here's the relevant snippet of my project's Maven 2 POM:
<build>
...
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<check>
<!-- Per-class thresholds -->
<lineRate>80</lineRate>
<branchRate>80</branchRate>
<!-- Project-wide thresholds -->
<totalLineRate>90</totalLineRate>
<totalBranchRate>90</totalBranchRate>
</check>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>coverage-tests</id>
<!-- The "verify" phase occurs just before "install" -->
<phase>verify</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
<instrumentation>
<excludes>
<exclude>au/**/*Constants.*</exclude>
</excludes>
<ignores>
<ignore>au/**/*Constants.*</ignore>
</ignores>
</instrumentation>
</configuration>
</plugin>
...
</plugins>
...
</build>
As I say, the coverage report works fine, the problem is that the "install" goal isn't failing as it should if the line or branch coverage is below my specified thresholds. Does anyone have this working, and if so, what does your POM look like and which version of Cobertura and Maven are you using? I'm using Maven 2.0.9 and Cobertura 2.2.
I've tried Googling and reading the Cobertura docs, but no luck (the latter are sparse to say the least).
To my knowledge, if the <haltOnFailure> element is set to true and any of the specified checks fails, then Cobertura will cause the build to fail which is what you're asking for. But actually, this element defaults to true if you do not specify it so you don't have to add it to your configuration checks. Failing the build below any coverage threshold is (or at least should be) the default behavior.
EDIT: I did some further testing and haltOnFailure seems to be working as expected on my environment (Maven 2.2.1. and versions 2.3, 2.2, 2.1 of the plugin i.e. versions 1.9.2, 1.9, 1.8 of cobertura on Linux). I'm updating this answer with the result below.
Actually, I've added an <execution> element to my pom. I may be misinterpreting the part of cobertura:check's documentation that says it "Binds by default to the lifecycle phase: verify" but, without the <execution> element, cobertura:check wasn't triggered during the verify phase of my build. Below the setup I've use for the cobertura-maven-plugin:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.3</version>
<configuration>
<check>
<!--<haltOnFailure>true</haltOnFailure>--><!-- optional -->
<!-- Per-class thresholds -->
<lineRate>80</lineRate>
<branchRate>80</branchRate>
<!-- Project-wide thresholds -->
<totalLineRate>90</totalLineRate>
<totalBranchRate>90</totalBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<!--<goal>clean</goal>--><!-- works if uncommented -->
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I get the following result when running mvn clean install on a freshly generated maven project (with mvn archetype:create) patched with the plugin configuration mentioned above:
$ mvn archetype:create -DgroupId=com.mycompany.samples -DartifactId=cobertura-haltonfailure-testcase
...
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building cobertura-haltonfailure-testcase
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory /home/pascal/Projects/cobertura-haltonfailure-testcase/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0-SNAPSHOT.jar
[INFO] Preparing cobertura:check
[WARNING] Removing: check from forked lifecycle, to prevent recursive invocation.
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/cobertura
Cobertura: Saved information on 1 classes.
Instrument time: 337ms
[INFO] Instrumentation was successful.
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [cobertura:check {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 1 classes.
[ERROR] com.mycompany.samples.App failed check. Line coverage rate of 0.0% is below 80.0%
Project failed check. Total line coverage rate of 0.0% is below 90.0%
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Coverage check failed. See messages above.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18 seconds
[INFO] Finished at: Sat Oct 24 21:00:39 CEST 2009
[INFO] Final Memory: 17M/70M
[INFO] ------------------------------------------------------------------------
$
I didn't test with maven 2.0.9, but on my machine, haltOnFailure generates a BUILD ERROR and halt the build. I don't see any differences with your plugin configuration, I can't reproduce the behavior you describe.
Add the following to the <check/> configuration.
<haltOnFailure>true</haltOnFailure>
mvn clean install -Dcobertura.skip=true

Categories

Resources