How to redirect unit test output Maven - maven

I'm working in a Java 8 project built using maven. Whenever I do a mvn install the root of my project gets polluted with the output files produced from my unit tests.
How can I redirect that output to somewhere else (maybe target directory) rather than the root directory of my project?
I thought about rewriting the unit tests to point the output to target but that seems a bit silly to me. Perhaps there is a plugin or a maven directive which might do what I want to accomplish?
I tried configuring the surefire plugin but this didn't help :(
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Set working directory for content -->
<workingDirectory>target/test-classes</workingDirectory>
<useFile>false</useFile>
<!-- Just set to some large numbers for all tests to work -->
<argLine>-Xmx1g -Xss1m -XX:MaxPermSize=128m</argLine>
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>

Looks like my initial attempt was almost there, just upgraded the version and it worked :)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<!-- Set working directory for content -->
<workingDirectory>target/test-classes</workingDirectory>
<useFile>false</useFile>
<!-- Just set to some large numbers for all tests to work -->
<argLine>-Xmx1g -Xss1m -XX:MaxPermSize=128m</argLine>
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>

There is new configuration option redirectTestOutputToFile since version 2.3 for exactly this purpose.

Related

Hot deployment failure with Maven jetty plugin on Windows

I have configured the Jetty Maven plugin to run my compiled war.
Here is the relevant part of my pom.xml.
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.50.v20221201</version>
<configuration>
<war>${jway.webapps.dir}/myapp.war</war>
<scanIntervalSeconds>2</scanIntervalSeconds>
</configuration>
</plugin>
If I execute mvn jetty:run-war, my war is build and Jetty serves the app as expected.
I have configured scanIntervalSeconds to allow hot redeploy.
However, if I rebuild using mvn install, I get the following error during redeployment:
java.lang.IllegalStateException: Failed to delete temp dir F:\...\myproject\target\tmp
at org.eclipse.jetty.webapp.WebInfConfiguration.configureTempDirectory (WebInfConfiguration.java:532)
at org.eclipse.jetty.webapp.WebInfConfiguration.resolveTempDirectory (WebInfConfiguration.java:424)
at org.eclipse.jetty.webapp.WebInfConfiguration.preConfigure (WebInfConfiguration.java:140)
at org.eclipse.jetty.webapp.WebAppContext.preConfigure (WebAppContext.java:488)
at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:523)
at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart (JettyWebAppContext.java:397)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
at org.eclipse.jetty.maven.plugin.JettyRunWarMojo.restartWebApp (JettyRunWarMojo.java:113)
at org.eclipse.jetty.maven.plugin.AbstractJettyMojo$1.filesChanged (AbstractJettyMojo.java:472)
at org.eclipse.jetty.util.Scanner.reportBulkChanges (Scanner.java:848)
at org.eclipse.jetty.util.Scanner.reportDifferences (Scanner.java:765)
at org.eclipse.jetty.util.Scanner.scan (Scanner.java:641)
at org.eclipse.jetty.util.Scanner$1.run (Scanner.java:558)
at java.util.TimerThread.mainLoop (Timer.java:555)
at java.util.TimerThread.run (Timer.java:505)
It seems that Jetty wants to delete the file, but Windows locks the file. In the plugin documentation, I have not found any configuration which seems to be helpful. Furthermore I have nothing found on Google. Is there any way to solve this issue?
I don't know if its relevant, but I do not use the jetty:run goal, because my war is build using a third party tool and I do not have a standard directory structure.
The jetty documentation contains a section about Troubleshooting Locked Files on Windows.
So I updated my plugin config according to the documentation:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.50.v20221201</version>
<configuration>
<war>${jway.webapps.dir}/myapp.war</war>
<scanIntervalSeconds>2</scanIntervalSeconds>
<webApp>
<_initParams>
<org.eclipse.jetty.servlet.Default.useFileMappedBuffer>false</org.eclipse.jetty.servlet.Default.useFileMappedBuffer>
</_initParams>
</webApp>
</configuration>
</plugin>

Maven : exclude target/generated-sources from compilation

This question, just to be sure my interpretation is correct :
I'm using Mojohaus jaxb2-maven-plugin to generate java classes from .xsd files, and by default it puts them in target/generated-sources
Now, I want to get track of these classes in source control (target is of course excluded), and I may one day slightly customize one with an annotation or a line of code, and I may even change my class generation plugin, so what do is I copy these classes and packages in src/main/java
This upsets Maven when I try to compile because he considers "target/generated-sources" as a source directory and he finds all clases twice. For what I understand, I can exclude classes inside a source directory, but I can't remove a source directory from Maven build, am I right ?
So the only solution would be to configure my jaxb2 plugin to generate the classes elsewhere, right ?
UPDATE :
Ok, this doesn't work as I thought, if I change the outputDirectory of my jaxb plugin, it's still included as a source directory by Maven, and I have no clue why.
<configuration>
<outputDirectory>${project.build.directory}/tatata/jaxb</outputDirectory>
</configuration>
UPDATE 2 : The explanation is the plugin is adding the outputDirectory as a maven source directory during the generate-sources phase of the build, and it's not optionnal or customizable.
First things first, do not add generation code to source control. Do not modify it manually. You will get into trouble. Believe me, I've seen it too many times. A new version of the schema and you're lost.
Ok, now to your question.
With maven-jaxb2-plugin you could turn off adding generation directory as a compile source root with:
<configuration>
<addCompileSourceRoot>false</addCompileSourceRoot>
</configuration>
Disclaimer: I'm the author of maven-jaxb2-plugin.
The answer from Lexicore is an interesting lead but my question was about the plugin I'm currently using, not how to do it with an other plugin.
So here is the workaround for the Mojohaus plugin : you can just skip the generate-sources by default (no need to do this task at every build when your model changes once in a week, then once in a year), and trigger it only when needed using a dedicated maven profile : How to skip generate-sources in Maven
you can always specify the target directory(generateDirectory) in pom config file as below. Hope it helps
`
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generateDirectory>${basedir}/src/main/java</generateDirectory>
<generatePackage>com.myproj.proxy</generatePackage>
<schemas>
<schema>
<!-- <url>${project.basedir}/src/main/resources/wsdl/test.wsdl</url> -->
<fileset>
<!-- Defaults to schemaDirectory. -->
<directory>${basedir}/src/main/resources/wsdl</directory>
<!-- Defaults to schemaIncludes. -->
<includes>
<include>*.wsdl</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
`

Varying plugin configuration for each child module

I have a multi-module build which contains modules which can target either Java 5 or Java 6. I want to allow modules to opt-in to Java 6, and leaving the default to 5.
To set Java 5 as a target I need to configure the following:
maven-compiler-plugin: source and target set to 1.5
maven-bundle-plugin: configure the Bundle-RuntimeExecutionEnvironment to J2SE-1.5
To set Java 6 as a target I need to configure the following:
maven-compiler-plugin: source and target set to 1.6
maven-bundle-plugin: configure the Bundle-RuntimeExecutionEnvironment to JavaSE-1.6
I considered having two properties: java.compiler.source and osgi.bree which can be defined by each module, but this leaves place for error.
How can I override the configuration of these two plugins per module with a single switch?
I would personally structure your project so that Java 5 modules descend from one parent POM and Java 6 modules from another parent POM.
Global Parent (majority of global settings)
Java5 parent (just define source/bundle)
module A
module B
Java 6 parent (just define source/bundle)
module C
How about allowing child modules to set a my.java.version property (or whatever you want it named) and embedding a Groovy script that sets version properties for the compiler and bundle plugins? Something like this in the parent pom:
<project ...>
...
<properties>
<my.java.version>1.5</my.java.version> <!-- default Java version -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<!-- set up properties in an early lifecycle phase -->
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- this can be as simple or complex as you need it to be -->
<source>
if (project.properties['my.java.version'] == '1.6') {
project.properties['my.compiler.version'] = '1.6'
project.properties['my.execution.environment.version'] = 'JavaSE-1.6'
}
else {
project.properties['my.compiler.version'] = '1.5'
project.properties['my.execution.environment.version'] = 'J2SE-1.5'
}
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- now use the properties from above in the plugin configurations -->
<!-- assume that both of these plugins will execute in a phase later than 'initialize' -->
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${my.compiler.version}</source>
<target>${my.compiler.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<!-- sorry if this part isn't correct; never used this plugin before -->
<instructions>
<Bundle-RuntimeExecutionEnvironment>${my.execution.environment.version}</Bundle-RuntimeExecutionEnvironment>
</instructions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I don't think there is an elegant Maven way to solve this complex scenario, neither yours or Duncan's proposed solution are easy maintainable IMO, when number of sub module becomes tremendous.
For maximum maintainability, I would write shell script (and/or batch file on Windows) in case Maven can't do the job very well, for example, a set-version.sh (and set-version.bat) that loop all sub module and reset the default java.compiler.source and osgi.bree properties based on a version-feed.txt, the version-feed.txt gives you a single central place for manipulating your version varying. As you can see, the cons is this is really not a Maven solution, it requires running set-version.sh before mvn ... every time version customization is required.
In addition, For build/release standardization, I would use maven-enforcer-plugin to play/pause the build process based on a property version.set(which is flagged by set-version.sh) and prompt some warning/error message if developer is not follow the correct procedure when doing build. The version.set also gives the flexibility if you prefer to use the default values defined in every sub module, instead of running set-version.sh, just directly set it to true in the parent pom.xml or from command-line parameter.
Sample directory structure:
parent/
module-a/
module-b/
module-c/
... ...
pom.xml
set-version.sh
set-version.bat
version-feed.txt
Hope this make sense.

maven 3 javadoc plugin doesn't take the excludepackagename config

I'm trying to exclude a bunch of packages from a javadoc site.
Unfortunately this plugin seems to live its own life and when it was configured as a report plugin it failed with access denied when moving files, so it was changed to be a normal plugin and then configured to run with the site goal (aggregated). By doing that we have the javadoc generated and it's published under the site as it should be.
But it seems that the configuration parameters for the plugin doesn't take effect at all. I've tried to move the <excludePackageNames> element around - both being a general config and to be a specific config for the aggregate goal - and I even added an exclusion for our entire code base and all files was still generated.
What I'm trying to do is to simply remove a couple of packages that shouldn't be in the javadoc. Anyone who got this plugin and the config to play nicely, to exclude packages?
This is the config I use right now, the javadoc is created but all packages, including the excluded, is generated.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<excludePackageNames>my.company.packages.*</excludePackageNames>
</configuration>
<executions>
<!-- Hook up the Javadoc generation on the site phase -->
<execution>
<id>aggregate</id>
<goals>
<goal>aggregate</goal>
</goals>
<phase>site</phase>
</execution>
</executions>
</plugin>
Any ideas, pretty please?
I solved identical problem by adding the sourcepath parameter to the configuration:
<configuration>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
<excludePackageNames>my.company.packages.*</excludePackageNames>
</configuration>
The configuration above will exclude all packages below my.company.packages but not my.company.packages itself. To exclude also my.company.packages use <excludePackageNames>my.company.packages</excludePackageNames> instead.

mvn release:perform automatically specify scm tag that includes release version

I would like to setup my maven release to run in batch mode, but I'm not a fan of the default scm tag ${artifactId}-${releaseVersion}. Instead, I'd like to simply tag it with ${releaseVersion}; however, I'm unclear if such a property exists (ie. without the -SNAPSHOT suffix).
I'd like the configuration to resemble the code below. Is such a default tagging possible with the maven-release-plugin?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
<configuration>
<tag>${releaseVersion}</tag>
</configuration>
</plugin>
I just got this to work when using Hudson to do my release. I noted that Hudson (with the Maven Release Plugin) is initiating the command with a property like -Dproject.rel.com.example:my-artifact-id=1.0.1. Using the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<tag>REL-${project.rel.com.example:my-artifact-id}</tag>
</configuration>
</plugin>
Resulted in the tag being REL-1.0.1
I'm new to the release plugin but I would assume something similar would work from the command line.
You can pass in the properties for:
releaseVersion -- What version you want it to be released as (1.0)
developmentVersion -- The next version (2.0-SNAPSHOT)
tag -- The name of the tag
a 1.0-SNAPSHOT implies a 1.0 release version, but doesn't set it. You can set that property in your POM file as a regular property.
try this:
<configuration>
<tag>${project.version}</tag>
</configuration>

Resources