PMD fails with custom rulesets for project with multiple modules - java

How to define custom-rulesets.xml in parent project, so it is re-used in children modules?
I have a PMD example project that works well (you can check later).
However, this maven project does not have children.
Project structure
pmd-java-14-example _.
|_ core
|_ tasks
custom-rulesets.xml
<?xml version="1.0"?>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#shortvariable -->
<ruleset
name="custom-ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>
<rule ref="category/java/design.xml/SimplifiedTernary"/>
<rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal"/>
<rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal"/>
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
<rule ref="category/java/codestyle.xml/DuplicateImports"/>
<rule ref="category/java/codestyle.xml/ShortMethodName"/>
<rule ref="category/java/codestyle.xml/ShortVariable"/>
</ruleset>
When I have a parent pom and a child module, pmd fails for the child module, because it cannot find custom-rulesets.xml.
pom.xml (parent pom that has a child module).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.yk.utils</groupId>
<artifactId>pmd-java-14-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>14</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<pmd.plugin.version>3.13.0</pmd.plugin.version>
<pmd.core.version>6.23.0</pmd.core.version>
</properties>
<modules>
<module>core</module>
<module>tasks</module>
</modules>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</reporting>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<targetJdk>${java.version}</targetJdk>
<rulesets>
<!-- https://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html -->
<ruleset>/rulesets/java/maven-pmd-plugin-default.xml</ruleset>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://github.com/pmd/pmd/blob/master/pmd-core/src/main/resources/rulesets/internal/all-java.xml -->
<ruleset>/category/java/bestpractices.xml</ruleset>
<ruleset>
custom-ruleset.xml
</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<id>check pmd and fail</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.core.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.core.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
The difference is that now the pom has child modules and <packaging>pom</packaging>.
This part fails mvn clean install
<ruleset>custom-ruleset.xml</ruleset>
Error messages
[INFO]
[INFO] pmd-java-14-example ................................ SUCCESS [ 0.922 s]
[INFO] core ............................................... FAILURE [ 1.929 s]
[INFO] tasks .............................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.980 s
[INFO] Finished at: 2020-05-03T13:15:45+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd (pmd) on project bst-core: Execution pmd of goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd failed: org.apache.maven.reporting.MavenReport
Exception: Could not find resource 'pmd-java-14-example\core\custom-ruleset.xml'. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
What I tried
As I understand, maven uses custom-rulesets.xml file for parent modules - it is successful, then it tries to use this file for child module, and of course, this file is not present in the child module.
I tried to set a property - absolute path to the location of custom-rulesets.xml. It works, however, I cannot push it into github.
I can also push this custom rule set to git hub and add a link to github repository and file and use it.
This is a better alternative.
And of course, I do not want to copy-paste this file across different children modules.

I would suggest that you place your rulesets outside the Maven projects, like you are doing for the other rulesets.
The question is whether your custom ruleset should only be checked for those child modules in that project. Or is it rather something you would like to check for all Java modules?
If you want to check all your Java modules, just add the ruleset with its absolute path to your existing PMD plugin configuration. The PMD plugin ignores POM projects.
If it's a specific ruleset for just that project and just those child modules, I would configure that in the parent project (the POM project).

Please, post your answers because this is a quick work-around which I need now!
<ruleset>../custom-ruleset.xml</ruleset>.
Explanation, my project structure is one parent that has two children modules. As the children are on the same level in the directory tree, ../ thus it works.
However, it will not work, if there is src folder in parent, or if a child module has children as well.

Related

Maven: pass the build even if one module fails

I am trying to generate an aggregate Jacoco code coverage report by following the steps here.
My initial maven project may contain multiple modules and I add an aggregator module aggregator to the main pom dynamically through my code.
The folder structure looks somewhat like this (there can be any number of modules and sub modules in the main project):
/SampleProject/
-- pom.xml
-- module1/
---- pom.xml
---- src/
-- module2/
---- pom.xml
---- src/
-- module3/
---- pom.xml
---- src/
-- aggregator/
---- pom.xml
This is the main pom:
SampleProject/pom.xml . (here, the aggregator module is added dynamically through my code)
<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.sample</groupId>
<artifactId>sampleProject</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>sampleProject</name>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>aggregator</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Each module has its own pom.xml. and the aggregator pom (which is generated by my code) looks something like this (each module is added as a dependency in this pom. This information is fetched from the individual poms of the modules):
aggregator/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>
<parent>
<groupId>com.sample</groupId>
<artifactId>sampleProject</artifactId>
<version>1.0</version>
</parent>
<artifactId>aggregator</artifactId>
<dependencies>
<dependency>
<groupId>com.sample.module1</groupId>
<artifactId>module1</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.sample.module2</groupId>
<artifactId>module2</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.sample.module3</groupId>
<artifactId>module3</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${argLine} -Xms256m -Xmx2048m</argLine>
<forkCount>1</forkCount>
<runOrder>random</runOrder>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and, to generate the aggregate report I use the following command:
mvn clean test -DskipTests=false
This generates the aggregate report in 'aggregator/target/site/jacoco-aggregate/index.html'
Now, the problem is if something goes wrong with only the aggregator build, and that build fails (but all other modules' build is successful). Then the maven output will look something like this:
[INFO] module1 .......................................... SUCCESS [ 2.197 s]
[INFO] module2 .......................................... SUCCESS [ 11.287 s]
[INFO] module3 .......................................... SUCCESS [ 6.969 s]
[INFO] aggregator ....................................... FAILURE [ 1.241 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 00:21 min
[INFO] Finished at: 2018-07-26T13:01:12-07:00
[INFO] Final Memory: 258M/1149M
Which means that the overall build is also marked as failed if just the aggregator part fails. This causes problem in the next step in the deployment pipeline.
So, what I want is that even if the aggregator build is failing, the overall build should be marked as passed (i.e. ignore the failing aggregator build) so that I can proceed with the next step in deployment, so that not generating the code coverage report, does not fail my entire deployment process. (I will handle the missing code coverage report later in the process).
Is there some way to achieve this? any help would be appreciated.
UPDATE 1:
I already check this answer and this is not what I want. Using -fae flag in maven would still mark the build as failed at the end.
Also, I do not want the -fn flag either because then I would be ignoring actual build failures in the modules as well, which is not desired.
What I'm looking for is a way to ignore the failures of just one module in maven.
I'm not really sure if doing that is possible, any alternate suggestions are welcome too.
UPDATE 2:
Update and some background info.:
So, I am building this tool as kind of a central tool which runs on all projects so it should be generic enough to cater to different types of configurations, without changing anything in the main project (on which the tests are being run).
As of now, it seems what I am looking for is not possible. So, as a workaround, I am asking the main project's owner to include a config file which tells my tool which packages to be included for code coverage.
Will update here if I find a better solution or workaround.

fatal error compiling invalid flag --module-path

I have a project. Originally it was a single module project with structure like this
java-cloud-sample\
src\
main\
java
pom.xml
I decided to make it into a multi-module structure - I use java 9 anyway.
So I separated it like this
java-cloud-sample\
java-cloud-rest-api\
src\
pom.xml
pom.xml
Where root pom.xml looks like this
<?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.lapots.breed.platform.cloud</groupId>
<artifactId>java-cloud-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>java-cloud-rest-api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And module pom.xml looks like this
<?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>
<parent>
<groupId>com.lapots.breed.platform.cloud</groupId>
<artifactId>java-cloud-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>java-cloud-rest-api</artifactId>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<encoding.source>${project.build.sourceEncoding}</encoding.source>
<encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting>
<java.source>${maven.compiler.source}</java.source>
<java.target>${maven.compiler.target}</java.target>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But when I try to run mvn clean package I get
INFO] java-cloud-rest-api ................................ FAILURE [ 1.060 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.104 s
[INFO] Finished at: 2017-09-08T17:15:46+03:00
[INFO] Final Memory: 27M/331M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.2:compile (default-compile) on project java-cloud-rest-api: Fatal error compiling: invalid flag: --module-path -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :java-cloud-rest-api
What is the problem? (maybe I should split maven project into separate modules and each maven module split into java modules)
This failure occurs when maven installed on your machine is configured with java8 as the default java version. You can confirm this by executing
mvn -version
on your terminal and checking the Java version stated in the configuration.
To resolve the version to a newer and supported version like java9 at the moment, you can create/edit the mavenrc(on MacOS) file on your machine:
vi ~/.mavenrc
to include these
export PATH
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/
export PATH=${PATH}:$JAVA_HOME/bin
Once you save this configuration, you can confirm maven should be using Java version 9 using the same command and then your project shall build without the stated error.
I had this error with the Maven compiler plugin 3.8.1 when a project was defining a module-info.java and Java 8 was used with Maven to compile the project. It seems that as soon a module-info.java is seen by the Maven compiler plugin it is adding the flag --module-path to the command line which is unknown to the Java 8 compiler. A way to exclude this file and compile the project without module support is to exclude the module-info.java from compilation based on a Maven profile:
<profiles>
<profile>
<id>java8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

How to get the maven shade plugin to use local modules in a multi-module project?

I'm trying to generate an uber jar in a multi module project with the maven-shade plugin with no success.
Versions: maven v3.9, maven-shade v3.0
The project looks as follow:
main
|- library
|- admin
|- ...
The sub module "admin" inherits (dependency) the sub module "library" and contains the maven-shade plugin definition. During the building process the plugin doesn't seem to find the POM of the local sub module "library".
Here are the poms
main pom.xml
<groupId>tapmeppe.server</groupId>
<artifactId>tapmeppe-server</artifactId>
<version>2017.1</version>
<packaging>pom</packaging>
<modules>
<module>library</module>
<module>admin</module>
<module>core</module>
</modules>
...
library pom.xml
<parent>
<groupId>tapmeppe.server</groupId>
<artifactId>tapmeppe-server</artifactId>
<version>2017.1</version>
</parent>
<artifactId>tapmeppe-server-library</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency><!--jdbc driver-->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency><!--JSON parser-->
<groupId>com.typesafe.play</groupId>
<artifactId>play_2.11</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
admin pom.xml
<parent>
<groupId>tapmeppe.server</groupId>
<artifactId>tapmeppe-server</artifactId>
<version>2017.1</version>
</parent>
<artifactId>tapmeppe-server-admin</artifactId>
<dependencies>
<dependency>
<groupId>tapmeppe.server</groupId>
<artifactId>tapmeppe-server-library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>admin-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<minimizeJar>true</minimizeJar>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>tapmeppe.admin.Starter</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is the output of the build
[INFO] Scanning for projects...
...
[WARNING] The POM for
tapmeppe.server:tapmeppe-server-library:jar:2017.1 is missing, no
dependency information available
...
[ERROR] Failed to execute goal on project tapmeppe-server-admin: Could
not resolve dependencies for project
tapmeppe.server:tapmeppe-server-admin:jar:2017.1: Failure to find
tapmeppe.server:tapmeppe-server-library:jar:2017.1 in
https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update
interval of central has elapsed or updates are forced -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
For some reason the plugin is looking for the dependency jar in the remote repository instead of using the sub module.
What am I missing? Thanks for the help.

Maven compilation error for android Project "error: package R does not exist "

I am trying to set up a MAVEN project with Android application.
I have this pom file
<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.myproject</groupId>
<artifactId>userprofile</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>userprofile</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
and during MAVEN compile I get this error (about 100 times, ie as many times as it is used in my methods)
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
src\main\java\com\myproject\userprofile\BaseActivity.java:[52,43] error: package R does not exist
Process finished with exit code 1
Any idea about this error? On the web I either find unanswered questions about similar error output. I have no experience on MAVEN, so I believe I am missing something here.
R class is build by your IDE during compilation. MAVEN cannot find the R class because by default the class can be found under build folder. You need to add something like this
<sourceDirectory>build</sourceDirectory>
<outputDirectory>target</outputDirectory>
telling MAVEN that you have some resource files under build folder and you want to make them available to compile so add them under the target folder which will be under your project.
so now I have
build
|----res
|----src
src
|----main
|----java
|----res
target
After a quick look, I believe you're missing the build element. Maven is building the project with current sources, R and other gen classes have not been generated yet. At least you need something like (after dependencies tag):
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.7.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<!-- platform or api level (api level 16 = platform 4.1)-->
<platform>16</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
Also the packaging must be apk:
<packaging>apk</packaging>
I would strongly recommend to start reading the Android Maven documentation along with samples.
This error might happen if you had modify the package name that generate by archetype:generate, for me, I use android-quickstart to generate the module structure :
mvn archetype:generate \
-DarchetypeArtifactId=android-quickstart \
-DarchetypeGroupId=de.akquinet.android.archetypes \
-DarchetypeVersion=1.0.11 \
-DgroupId=com.yy.android.gameLibs \
-DartifactId=sample
akquient recommend me to use "com.yy.android.gameLibs" as package name and I accepted, I compile this module successful and worked. After that, I change the package name as "com.yy.android.sample" also change the Androidmenifest.xml package attribute, therefore the module report that error, I follow back the generate command and choice package name myself to solve this.
Modify packaging to apklib, like this:
<packaging>apklib</packaging>
And add build goal at end of pom.xml like this:
<dependencies>
<!--Android deps -->
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.0.1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<sdk>
<platform>17</platform>
</sdk>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>

Maven: javax.servlet specification deployed

I've a web application configured with Maven which uses a library, also configured with Maven and when I package geronimo-servlet_3.0_spec-1.0.jar is included in WEB-INF/lib and I don't understand why.
I check the library with mvn dependency:tree
$ mvn dependency:tree | grep geronimo
[INFO] +- org.apache.geronimo.specs:geronimo-servlet_3.0_spec:jar:1.0:provided
I check my web app:
$ mvn dependency:tree | grep geronimo
$
However when I run mvn:package the file gets included in WEB-INF/lib.
When I run mvn tomcat:run I can see:
INFO: validateJarFile(/home/stivlo/workspace/private/src/main/webapp/WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Why and how to avoid? Thank you.
UPDATE 1: as requested I add 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.obliquid</groupId>
<artifactId>test-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>private webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!-- For Jakarta ORO -->
<repository>
<id>mvnsearch</id>
<name>Maven Search</name>
<url>http://www.mvnsearch.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>private</finalName>
</build>
</project>
UPDATE 2: I followed the advice of Stephen C and modified the build section as follows:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war</artifactId>
<version>2.1</version>
<configuration>
<overlays>
<overlay>
<groupId>org.obliquid</groupId>
<artifactId>test-webapp</artifactId>
<excludes>
<exclude>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
However geronimo*.jar still gets included. I guess I've made a mistake in this configuration.
UPDATE 3: Stephen C. says that I should use
the groupId the artifactId of the WAR
file that contains the JAR file(s)
that you are trying to exclude.
I didn't know that WAR files could have a groupId and artifactId, in fact in my pom.xml I don't see any. My project builds a WAR file and has a groupId and an artifactId and those were the ones I tested above without success.
The dependency causing the problem is the following (is a JAR, not a WAR):
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
If I try to use the groupId and artifactId listed in this dependency I've the following error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.obliquid.helpers:obliquid-helpers]
is not a dependency of the project. ->
[Help 1]
If I try to use the groupId and artifactId of the JAR included by org.obliquid.helpers:
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>)
I have the same error.
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.1.1:war
(default-war) on project test-webapp:
overlay [ id
org.apache.geronimo.specs:geronimo-servlet_3.0_spec]
is not a dependency of the project. ->
[Help 1]
Reading the War plugin documentation, I found a section about creating skinny WARs. So I tried the following:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Still without any success, geronimo-servlet_3.0_spec-1.0.jar is still there!
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.obliquid.helpers:obliquid-helpers] is not a dependency of the project. -> [Help 1]
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project test-webapp: overlay [ id org.apache.geronimo.specs:geronimo-servlet_3.0_spec] is not a dependency of the project. -> [Help 1]
UPDATE 4: I discovered that the target/private.war file is not a zip of target/private/ directory, but the exclusions are done at packaging time and not by deleting files in target/private/ -- This means, I've to re-test all the things I did before.
Suggestion of gouki: doesn't work, the JAR is still there also in the WAR file.
Suggestion of Stephen C., maybe mis-understood: actually I just noticed that the pom.xml is always invalid whatever groupId/artifactId I put of the three possibilities explained above. So they didn't work for me.
What I found in the documentation (packagingExcludes), works.
Now, if I had to choose one of he answers I would choose Stephen C., because he helped me pointing at the documentation of the WAR plugin (I was reading in the wrong places). However I'd accept an answer that doesn't work, at least in the way I tried (probably wrong). So I'm not going to accept any answer, and add a new answer myself with the final working configuration.
UPDATE 5: I post the relevant part of the pom.xml of obliquid-helpers, that mentions geronimo-servlet_3.0_spec. I've marked it optional and with scope provided, still it gets included by a web-app, unless I mark it as "packagingExclude" in the maven-war-plugin configuration.
<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.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<packaging>jar</packaging>
<name>obliquid-helpers</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
[...]
</repositories>
<dependencies>
[...]
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Clearly, something has a dependency on that JAR file. If it is not showing up in the dependency tree, perhaps it is due to a dependency of your webapp WAR file on another WAR file that has this dependency.
If that is the case, then you could get add an <excludes> to the <overlay> element of the build descriptor for the WAR file plugin; e.g.
...
<build>
<finalName>webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<overlays>
<overlay>
<groupId>xxx</groupId>
<artifactId>yyy</artifactId>
<excludes>
<exclude>WEB-INF/lib/whatever.jar</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
If you are using WAR file overlays, you should always include the clean target in the build. Otherwise you can get old dependencies hanging around in the WAR file. (IIRC, there is a Warning in the Maven output each time you build an overlaid WAR without cleaning!)
In fact, this could be the root cause of your problems. For instance, if previously you had the "geronimo" as an ordinary dependency and you haven't run mvn clean since then, the JAR file could still be hanging around.
Based from your pom.xml, the only dependency that might have dependency on geronimo servlet is
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Can you try excluding the geronimo in this dependency?
<dependency>
<groupId>org.obliquid.helpers</groupId>
<artifactId>obliquid-helpers</artifactId>
<version>0.9-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
First I want to thank gouki and Stephen C. for helping me. However their proposed solution didn't work for me. I'm grateful to them, but I can't accept their answer, because it would be misleading since it didn't work for this problem. I've upvoted Stephen C. answer, because he pointed me to the right documentation, which was essential to solve the problem.
Reading the WAR plugin documentation, especially the war:war mojo section, I've found an example on how to create Skinny WARs, which did the trick. So here is below the working configuration, to be added to the build section:
<build>
<finalName>private</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
The archive part is probably not really needed, but I will find out when I deploy the WAR. The part that does the trick is the packagingExcludes tag, that can contain a comma separated list of tokens to exclude from the WAR before packaging.

Categories