Compiling selected file with maven from command line [duplicate] - java

I want to compile only selected files or directories (including subdirectories) within source directory. I was pretty sure I can do this using <includes> of maven-compiler-plugin's configuration, but it seems to not work as I expect since it still compiles all classes into target/classes. What is really strange, Maven output suggest that the setting actually does its work, because with:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>com/example/dao/bean/*.java</include>
</includes>
</configuration>
</plugin>
I have:
[INFO] Compiling 1 source file to c:\Projects\test\target\classes
but with no compiler's configuration I have:
[INFO] Compiling 14 source file to c:\Projects\test\target\classes
In both cases however, all 14 classes are compiled into target/classes as I mentioned. Can you explain that or suggest another solution to compile only selected files?

Simple app with 3 classes.
com/company/Obj1.java
com/company/Obj2.java
com/company/inner/Obj3.java
build in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>com/company/inner/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
result: 1 class is compiled.
And any combination of includes is working well
or you mean something else?

I have faced a similar situation. We needed to hot swap only modified files to our remote docker container in order to improve changes-deploy time. This is how we solved it.
Add includes option in build plugin with command line argument.
Note since we wanted to add multiple files, so we have used includes and not include
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
<includes>${changed.classes}</includes>
</configuration>
</plugin>
Now run compile phase with argument, example:
mvn compile -Dchanged.classes=com/demo/ClassA.java,com/demo/ClassB.java,com/demo2/*

maven-compiler-plugin using Ant-like inclusion/exclusion notation.
You can see examples in Ant documentation Ant FileSet Type
If you are want include only files from one directory, you need write it like you did:
<include>com/example/dao/bean/*.java</include>
To include also subdirectories from path, it will be:
<include>com/example/dao/bean/**/*.java</include>

I had no difficulty including or excluding files for compilation with maven compiler plugin 2.5.1. Here is the dummy project I used for the purpose. Perhaps the include pattern that you use is different.

Related

Is there a way to load Maven Surefire Plugin exclusions list from a file?

In the Surefire plugin, you have have an exclusions list of files (or regex) like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
I want to add a bunch of files to my exclusions list that don't match a regex. This list is about 200 files (an eccentric collection). I'd like to add those files from a file external to my Maven pom.xml.
I think there is a way to do this using the maven properties plugin. I can't see how that would load a variable into a list though.
Is there a way to load the exclusions list from a file?
The Surefire plugin provides exactly the configuration element for this, called excludesFile:
A file containing exclude patterns. Blank lines, or lines starting with # are ignored. If excludes are also specified, these patterns are appended. Example with path, simple and regex excludes:
*/test/*
**/DontRunTest.*
%regex[.*Test.*|.*Not.*]
It was introduced in version 2.19 of the plugin as part of SUREFIRE-1065 and SUREFIRE-1134. As such, you could directly have a file called surefireExcludes.txt at the base directory of your project, containing
**/TestCircle.java
**/TestSquare.java
# **/TestRectangle.java A commented-out pattern
and then configure the plugin with:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludesFile>surefireExcludes.txt</excludesFile>
</configuration>
</plugin>
All of the tests will be run, except for those matching the patterns specified in the file. The patterns can be even commented-out if they start with #. This could also be passed directly on the command-line with the surefire.excludesFile user property, i.e.:
mvn clean test -Dsurefire.excludesFile=surefireExcludes.txt
The parameter includesFile also exists for the opposite requirement.

How to specify JAR location of a Maven dependency as plugin parameter?

I've tried to follow the configuration from this response on SO to use the jar location in my local repo as a plugin parameter, but it doesn't seem to work. I don't know if this due to a newer Maven version than the response (I'm using Maven 3.2.5).
In my pom.xml, I need to add a javaagent to my surefire plugin definition. The javaagent jar file is a dependency in my project.
I have tried the following:
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<!-- Configuration to use jmockit on IBM J9 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
I was expecting that the ${org.jmockit:jmockit:jar} would be expanded to the location of the jar, but in my mvn console I see the following error:
[ERROR] Command wascmd.exe /X /C "C:\IBM\SDP\jdk\jre\bin\java -javaagent:${org.jmockit:jmockit:jar} -jar C:\dev\Eclipse\rtc-connector\target\surefire\surefirebooter1389906134960134.jar C:\dev\Eclipse\rtc-connector\target\surefire\surefire5488684370604495471tmp C:\dev\Eclipse\rtc-connector\target\surefire\surefire_05402037720997438783tmp"
So obviously the parameter is not getting expanded. I was hoping/expecting to see something like -javaagent:c:\users\eric\.m2\repository\org.jmockit\1.20\jmockit-1.20.jar or something similar.
Is there a clean way I can reference the jar from my dependency in my plugin configuration? I know I can use the dependency-plugin to copy the jar to a known location in my target folder and then point to that, but I was hoping there would be an easier solution that doesn't require the intermediary step.
To be variable expanded need to add maven-dependency-plugin:
<!-- obtain ${*:*:jar} properties -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
You need to set the "argLine" tag value as follow :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<argLine>
-javaagent:"${localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar"
</argLine>
</configuration>
</plugin>
You need to set the .m2 repo path where the jmockit jar is present. It's working for me.
The maven-dependency-plugin contains a goal build-classpath which would solve this problem.
On command line you can do things like this:
mvn dependency:build-classpath -DincludeArtifactIds=testng -DincludeGroupIds=testng
which results in:
C:\repository\org\testng\testng\6.8.21\testng-6.8.21.jar
The resulting classpath can also be put into a property outputProperty ....
This could be configured into pom as well...

Run a single java package on eclipse using maven

I have multiple java packages in my java project. Is it possible to run a single package on eclipse using maven. I want to do it with the project level pom. I dont want to create POMs for every package.
Based on your comment, not sure if this is what you are looking for but I use a configuration variable to run test cases for a particular package. You have to add a plugin to the POM to achieve this.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<includes>
<include>**/${testGroup}/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now if you want to run the test cases for lets say package org/myPackage you would use a command like this.
mvn test -DtestGroup=org/myPackage

mvn clean install do not use last .class version

It seems Maven keep using use an old version of my code when packaging a war archive.
I build my war using a simple "mvn clean install".
I deleted the /target folder by hand and checked that there were no .class elsewhere in my project (as described here).
Each time I check the generated archive, an old version of my code has been packaged inside. Yet, the same code is properly packaged on my colleague's machine (he uses m2e).
Does anyone have the same problem ?
Here is the most relevant part of the POM:
<packaging>war</packaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>2.3.2</version> -->
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
It appears that the old code is still present somewhere; my guess would be your local repository. Try deleting any entry there that could contain the old code and then building your project again.
Check the <dependency> entry in pom.xml for that jar file. Change the <version> of that dependency to the latest one.

How can I switch the bytecode target level in IntelliJ using Maven 3 profiles

I am using maven profiles to switch between two "setups" in Intelli J. How can I switch the bytecode target level? That is: I want to change the following setting in compiler.xml
<bytecodeTargetLevel>
<module name="finmath-lib" target="1.6" />
</bytecodeTargetLevel>
Note: I tried to perform this via the following part in the respective Maven 3 profile
<profile>
<id>java-8</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
but this does not work!
Note: The pom.xml etc. belongs to the finmath lib project, and if you are interested, it can be found at www.finmath.net
I am using IntelliJ IDEA 14.1.3 and it works with following config ...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Hope this is helpful. Thanks!
The problem is that you want to change it to 1.8.
I've tried to switch IntelliJ's compiler version between 1.7 and 1.6 or 1.5 using Maven profile successfully.
However when you want to change it to 1.8, the config will be ignored.
I don't know whether this is maven's problem or Intellj's.
It seems that the JDK 1.8 is not well supported now, which is understandable.
You can open the Maven Projects tool window in IntelliJ and select the profile you want to use from the list.
In IntelliJ 13, File > Project Structure, set the Project language level to 8.0.

Categories

Resources