Failed to bypass maven checkstyle plugin - java

My project is consisted of two modules, one is common, the other let's call it X.
When using maven to "clean install" module X, it complained:
You have 50 Checkstyle violations.
Looking into the pom.xml I didn't see any checkstyle maven plugin, but in the "common" module I did find it in "common"'s pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
</plugin>
Since module X depends on module "common", I updated common's checkstyle plugin in pom.xml like this:
<configuration>
<skip>true</skip>
</configuration>
and rebuilt the common module before I ran the "clean install" on X module.
To play safe, I added one more setting on the mvn command when running X module:
mvn clean install -DskipTests -Dcheckstyle.skip
However, it looks that neitherthe change in "common"'s pom.xml w.r.t checkstyle plugin nor the additional setting on mvn command had any effect on the running - it still complained the same.

I tried with, pluginManagment and it works fine. You need to add this in your parent pom file.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>

It turned out that there are two places in the pom.xml that involve checkstyle plugin: one is under the tag
<pluginManagement>
, in which I added:
<skip>true</skip>
However it's not sufficient (at least in my case), I found the checkstyle was also registered under:
<build><plugins>
it looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.1.1</version>
</dependency>
</dependencies>
<configuration>
<skip>true</skip>
....
</configuration>
<plugin>
Now after I added skip being true to both places and rebuilt the project modules, the errors were gone.

Related

How to add maven-checkstyle to existing project?

I tried adding the maven-checkstyle-plugin to my project, but it does not produce any results:
<project>
....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn checkstyle:checkstyle or mvn checkstyle:check results in:
[INFO] You have 0 Checkstyle violations.
Which is essentially not true, because I added violations explicit in my code.
So my guess is that checkstyle is not running the analyser properly over my code (that resides just normal in /src/main/resources).
What could I do?
Would I maybe have to add the google_checks.xml file explicit to my classpath??

How to compile a Maven JAR file with JVM args use a plugin?

I'm looking for a Maven plugin that could execute by itself the good JVM arguments.
For now, I've tried using the maven-compiler-plugin and the maven-surefire-plugin (the pom of my project is down below).
the type of argument I want the jvm to execute is this one:
--module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls
in order to launch a javafx app.
the pom.xml with the plugin tests.
<build>
<plugins>
//tests with the surefire plugin (without tests).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skipTests>true</skipTests>
<argLine>--module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls</argLine>
</configuration>
</plugin>
</plugins>
</build>

Create a simple JAR instead of an executable JAR with Spring-Boot

I have a problem with a Multi-Module Spring-Boot Application.
I have one Module that I use for Services, the core-Module. And one module for View-Related Classes, the web-Module.
The are both in a parent-Module, where I add the dependencies, like the "spring-boot-starter" and that can be used by both modules.
Now to the problem:
I want to run the web-Module with the embedded Tomcat and have the core-Module as a dependency in the web-module.
In other Spring projects I would just include the maven-jar-plugin and create a jar of the core-Module.
The problem in this Spring-Boot project is that the maven-jar-plugin is already configured, in the "spring-boot-starter". And it needs a mainClass, which only the web-module has.
Small excerpt from the "spring-boot-starter"-POM
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
Is there a way to package the core-Module as a JAR without needing a "start-class" in that module?
It seem like you can disable the fat-JAR from replacing the original and getting installed into the repo by configuring the spring-boot-maven-plugin.
Taken from http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/maven-plugin/examples/repackage-disable-attach.html:
By default, the repackage goal will replace the original artifact with
the executable one. If you need to only deploy the original jar and
yet be able to run your app with the regular file name, configure the
plugin as follows:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
This configuration will generate two artifacts: the original one and the executable counter part produced by the repackage goal. Only the original one will be installed/deployed.
You can just disable the spring-boot-maven plugin this way.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
I have found the answer.
I configured my application just like #judgingnotjudging explained. The difference was that I had put this, in the parent-POM:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
It was preventing the default creation of JARs in the children-modules. I could resolve this problem by including this only in the web-Module.
That way Spring-Boot builds a fat-JAR from the web-Module and a simple JAR from the core-Module.
To create simple jar update
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
To
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
For more details please visit below Spring URL:-
https://docs.spring.io/spring-boot/docs/1.1.2.RELEASE/reference/html/howto-build.html
You can simply change
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
to
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
I don't know whether it's still relevant or not, however, a simple classifier needs to be configured for the spring-boot-maven-plugin -
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
This would generate two jars - one a normal one which can be included as a dependency, another as an exe jar with the "exec" word appended as a suffix - like test-0.0.1-SNAPSHOT-exec.jar and test-0.0.1-SNAPSHOT.jar
If you're using the embedded tomcat to run your app, don't you just want a standard Spring Boot fat jar for your web app? If so, just mark your web module as dependent on your core module in the pom, and build your project.
It's there a different use case that you need the jars separated?
Are you building your modules as completely separate modules or as modules as part of a single multi-module project? The difference is in the latter, you will have a pom at the root specifying the modules. I forget the Maven syntax specifically, so my example is Gradle (Maven docs for multi-module builds are here). Sorry about that.
baseProject
|----web-module
|----core-module
baseProject's build.gradle:
project(':web-module') {
dependencies {
compile project(':core-module')
}
evaluationDependsOn(':core-module')
}
Maven has a similar structure. You should review the docs, but I believe all you need to do is specify the module order correctly in your parent pom as below and include the dependency in your web-module pom.
Parent pom:
...
<modules>
<module>core-module</module>
<module>web-module</module>
</modules>
web-pom:
<project ...>
<parent>
<groupId>com.example</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>web-module</artifactId>
<packaging>war</packaging>
<name>My web-module</name>
<dependencies>
...
<dependency>
<groupId>com.example</groupId>
<artifactId>core-module</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
...
core pom should only need to include the parent section as in the web-module above, and otherwise be the same.

Dependency injection and Tests

I have a project that contains some tests, all it does is create a test-jar to be run by other projects. And it depends on a another artifact, let's call it artifact X
then there are multiple other projects that run the test-jar, using the maven-surefire-plugin
I have multiple implementations of artifact X and what I want to do is to be able to run those tests using the implementation that I want, not X
so for example:
in project A I want to run the tests using implementation A
in project B I want to run the tests using implementation B
this does not seems to be possible, because once the test-jar generated, it has the artifact X hard-coded in it
so I'm open to any other solutions
Edit :
extra information:
the pom file of the project generating the tests :
the part for generating the test-jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
the Artifact X dependency :
<dependency>
<groupId>com.conztanz</groupId>
<artifactId>X</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
the pom file of one of the projects running the tests (project A):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<dependenciesToScan>
<dependency>testJar</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>
Ideally what I want is do some thing that tells project A to Override artifact X with it's own implementation
I hope this makes more sense now
I think you're looking for the scope called provided.
The dependency would look like this:
<dependency>
<groupId>com.conztanz</groupId>
<artifactId>X</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
This tells Maven: "Hey, I need this to compile, but don't include it in the target output, because a container (or something) will provide the jar itself."

Getting Maven site plugin working when in profile of multi-module project?

I am attempting to build a full site for my multi-module project in one command that I can execute in Jenkins. However when I build it, the submodule javadocs appear in the site root (site/apidocs instead of site/submodule/apidocs). All the links from the parent to submodule are broken as well.
Organization is the standard
pom.xml
submodule/pom.xml
Parent POM contains:
<build>
<pluginManagement>
<plugins>
... Various unrelated plugins ...
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>complete-build</id>
<build>
<plugins>
<!--JavaDoc setup for Jars-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<defaultAuthor>Leon Blakey</defaultAuthor>
<defaultVersion>${project.version}</defaultVersion>
<sourcepath>target/delombok</sourcepath>
</configuration>
</plugin>
<!--Deploy site with Mercurial (Hg)-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<dependencies>
<dependency><!-- add support for scm -->
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-scm</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-hg</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
<configuration>
<reportPlugins>
<!--JavaDoc setup for Site-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<defaultAuthor>Leon Blakey</defaultAuthor>
<defaultVersion>${project.version}</defaultVersion>
<sourcepath>target/delombok</sourcepath>
<show>public</show>
</configuration>
</plugin>
... Checkstyle, PMD, Findbugs, etc ...
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The submodule POM just contains the skeleton plugin definitions for pluginManagement.
When I try to build a site (both on my local machine and Jenkins) I run from the parent
mvn clean install site-deploy -Pcomplete-build
With the above setup, what would cause a submodule to dump its site files in the site root (site/) instead of the submodule directory (site/submodule)? Do I need to use the stanging commands (which I'm avoiding since it breaks the release process)? Is it simply impossible to build a multi-module site in one command?
Use the maven-reactor-plugin to trigger building at the submodule level.

Categories

Resources