How to run Maven Checkstyle plugin on modified files only - java

I am running Maven Checkstyle plugin through pre-commit githook written in python (I think that the same question applies to running it directly from terminal).
The command is:
mvn checkstyle:checkstyle
However, I would like to run Maven Checkstyle only on files modified by git. For instance, I could run it once for each file. If I want to run it on a specific file, I may define the following pattern (I am not sure why do I need the pattern of stars and dashes in front):
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*File.java
However, I am not able to pass file path and file name, for instance:
mvn checkstyle:checkstyle -Dcheckstyle.includes=src/main/java/File.java
Or, following the above mentioned pattern:
mvn checkstyle:checkstyle -Dcheckstyle.includes=**\/*src/main/java/File.java
I have tried many other combinations as well, but nothing works. I found this issue which is about the same thing, so I am wondering if someone has found a solution to this.
I have also read:
How to run maven checkstyle plugin on incremental code only
Is there a maven-git-checkstyle plugin that runs checkstyle goal on git staged files alone?
How to check style arbitrary list of java files from command line?
but they do not solve my problem.

There is an example here: github project of maven-checkstyle-plugin
Put a placeholder in pom and pass the parameter by maven command line.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>${checkstyle.includes}</includes>
</configuration>
Command line: mvn checkstyle:check "-Dcheckstyle.includes=**/File.java"

Related

How to exclude specific flies from mvn build path from command line?

while executing mvn clean install -DskipTests I want to exclude a file com.java.test.Test.java from build path(as it has some error). How to do that?
You can use the <exclude> tag in your pom.xml to exclude files in Maven. For more information see https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Another way is adding the class to an exclusion pattern as an argument like:
mvn clean install -Dtest="*,!Test"
For more information see
Skip single test with maven with command line options

Not able to do maven release with jasypt encryption

I've a Springboot project handled by maven that contains some secrets encrypted with Jasypt. When I'm running the mvn deploy I'm passing the jasypt password as:
mvn -B clean deploy -Djasypt.encryptor.password=${jasypt_password}
And it is able to run test cases and deploy the jar file to repository. But when I'm doing the same with mvn release the jasypt password is not properly set.
mvn -B clean release:prepare release:perform -Djasypt.encryptor.password=${jasypt_password}
Or
mvn -B release:prepare -Djasypt.encryptor.password=${jasypt_password}
For both these cases, I'm getting following error while running the test cases.
Caused by: java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password
The plugin configuration I'm using is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<localCheckout>true</localCheckout>
</configuration>
</plugin>
How should I set jasypt password while running the maven release?
I got the issue solved using following command. Apparently maven release plugin takes configuration parameters in a different way.
mvn -B clean release:prepare release:perform -Darguments="-Djasypt.encryptor.password=${jasypt_password}"
You can check this link which gives more details about the issue.
Basically it states that
Once you enable jasypt-spring-boot the password property is required
as specified in the documentation
They have suggested multiple solutions. One of them being:
Add jasypt.encryptor.password=dummy to your springboot properties file.
And another (if running from command line), run your jar with
-Djasypt.encryptor.password=*******************
If you are using Intellij,
Using
-Djasypt.encryptor.password=${jasypt_password} as Project argument should resolve your issue.
If it doesnot help then try
--jasypt.encryptor.password=${jasypt_password}.
Let me know if this helps.
Thanks,
Manu

Find Java options used by Maven

How can I find which Java options (Xmx, Xms, Xss, etc) are being used by Maven?
I've found out that a way to set them is via the environment MAVEN_OPTS. Now I want a way to be assured it's getting the right settings.
EDIT: I believe it´s different to this question as I don´t want to see the value of an environment variable. I rather want to see which settings are actually being used by Maven, whether it comes from a env var, settings.xml or other means.
EDIT2: I'm also interested in other ways of setting Java options for a Maven build
You can set Java options for Maven in different points and level (globally or via plugins configuration):
Plugin configuration: just for Compilation
Using the Maven Compiler Plugin configuration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs configuration entry, available for both compile and testCompile goals. An official example is available here and on other SO answers like this one.
An example is also shown below on the third point.
Plugin configuration: just for Tests execution
Using the Maven Surefire Plugin configuration for tests executions, you can set the required Java options to be used at runtime via the argLine configuration entry of the test goal. An official example is available here.
An example is also shown below on the third point.
Plugin configuration: via Properties (and profiles)
You can combine the two options above (in case of common Java options) as a property value to pass to both compileArgs and argLine configuration entry or have different properties per configuration (according to your needs).
<property>
<jvm.options>-Xmx256M</jvm.options>
</property>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
Using properties gives you also an two extra advantages (on top of centralization): you can use profiles then to personalize it based on different desired behaviours (and example in this SO answer) and you can override them via command line as well, like:
mvn clean install -Djvm.options=-Xmx512
Global/Project configuration: Options file
Since Maven 3.3.1, you can specify your options in a .mvn/jvm.config file per project. It's an alternative to MAVEN_OPTS and a more narrowed scope (per project). However, since it sis a new Maven feature, people should be aware of it, otherwise troubleshooting and maintenance may be impacted.
Global/Env configuration: MAVEN_OPTS
Maven well-known environment variable to set global execution options, however applied to all Maven builds sharing that environment (i.e. per logged user).
When running Maven using the -X option (debug enabled), you will have the following output as part of your build:
[DEBUG] properties used {java.vendor=Oracle Corporation, ... , env.MAVEN_OPTS=-Xmx256M, ...
Update
After all, the executed mvn command is an OS script. Having a look at it in Windows, I found the possibility of using the MAVEN_BATCH_ECHO option which, if enabled (value set to on), will echo any command executed by the script and as such also the invocation of the java command, where you can see if your options (the MAVEN_OPTS) are picked up correctly together with the full list of parameters passed to it.
Here is an execution I tested on Windows:
set MAVEN_BATCH_ECHO=on
set MAVEN_OPTS=-Xmx256M
mvn compile > output.txt
NOTE: the output.txt will contain quite a lot of text, providing build output and additional echos executions. As part of it, it provided:
>"path_to_\jdk1.7\bin\java.exe" -Xmx256M -classpath "path_to\apache-maven-3.1.1\bin\..\boot\plexus-classworlds-2.5.1.jar" "-Dclassworlds.conf=path_to\apache-maven-3.1.1\bin\..\bin\m2.conf" "-Dmaven.home=path_to\apache-maven-3.1.1\bin\.." org.codehaus.plexus.classworlds.launcher.Launcher compile
As you can see, the -Xmx256M option was picked up correctly. If the maven script for other OS doesn't provide this option, then you can simply add it to the script (a simple echo before the java execution, showing the command, would also be enough).
You can find the maven script in your maven installation folder, under the bin subfolder.
Update2
Furthermore, since Maven is a Java tool after all and as you can see from its script it invokes the java command, you could see all the available options as suggested in this SO answer by slightly changing the Maven script and use the jinfo command which should really give you the answer according to this other SO answer.
Maybe it helps to start Maven with verbose debug output (-debug, I think?). Otherwise just do a ps aux | grep java and check the arguments of the process (assuming *nix).

Maven + Clover + Jenkins - How to get Coverage report and non-instrumented artifact in one command

I am trying to run maven clover plugin to generate report as well as generate NON-instrumented artifact.
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>3.1.3</version>
<configuration>
<generatePdf>true</generatePdf>
<generateHtml>true</generateHtml>
<licenseLocation>clover.license</licenseLocation>
<!-- the contextFilters element has to be specified within the reporting section and will not work if you specify it in the build section. -->
<!-- contextFilters>try,static,catch</contextFilters -->
</configuration>
</plugin>
mvn clean clover2:instrument clover2:clover install
If I run above according to clover doc instument goal will run in separate lifecycle and will not affect default buildcycle. So It does but problem is I want to skip test during default build lifecycle.
I tried following but it skipped test for both lifecycle.
mvn clean clover2:instrument clover2:clover install -DskipTests
If above works then I can simple set it up on jenkins withou creating mulitple jobs for multiple maven commands.
It is probably not the best idea to do everything in single cryptic maven command (in the same way it is not the best idea to put all your code in a procedure). Why not splitting the command into several steps or even jobs, which will trigger one another? Moreover from CI point of view different kind of jobs ask different priority to fail fast. I do understand that it is not exactly an answer.

Running main classes from a deployed artifact with maven

I don't get it. I've set up my pom.xml to use the Maven exec plugin so I can execute some of the classes in my project with the correct classpath, -D defines and -javaagent. So from a shell with the classes built in ./target/classes etc.. I can run the main() methods using
mvn exec:java -Dexec:mainClass=classWithAMainMethod
All good so far.
Now I want to ship my project(a jar artifact) and I still want to be able to use the configuration I've put in the pom.xml for running the classes with the correct arguments etc.. How do I do it? Is there some way of staying
mvn -artifactJar=MyArtifact.jar exec:java -Dexec:mainClass=classWithAMainMethod
when all I have is MyArtifact.jar(Or a maven repository with MyArtifact.jar in it)??
I've tried the following:
Get the jar with the dependency:get goal and unzip it. I can't do anything with it
as the pom.xml seems to end up in META-INF/maven in the artifact jar. Is there any way of using it?
Creating a dummy pom where I want to run my project with a single dependency on my projects artifact. I can then use exec:java to run the main classes but it's dosen't uses the configuration from my projects pom.
Thanks.
The AppAssembler plugin worked out quite well for me. I replaced the exec plugin config in my project's pom with something like this in the build section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<extraJvmArguments>
-Djava.rmi.server.hostname=localhost
-javaagent:${spring.javaagent.jar}
</extraJvmArguments>
<programs>
<program>
<name>foo1</name>
<mainClass>net.foor.FooMain</mainClass>
</program>
...
</configuration>
</plugin>
In Eclipse I created an external tools launcher to run the resulting scripts from target/appassembler/bin
On the machine I wanted to deploy to(Assuming access to the internal Maven repository where my artifact+dependencies have been installed/deployed):
First use wget or mvn dependency:get to get a copy of my artifact jar.
Extract the pom. unzip -j artifact.jar */pom.xml*
Run mvn appassembler:assemble -DassembleDirectory=.
Move the artifact.jar into the ./lib directory
Set execute permissions on generated shell scripts in ./bin
Have you tried using something like onejar?
That sounds like what you're looking for.

Categories

Resources