clean a folder contents during pre-integration-test phase - maven

I have parent pom where I'm trying to unpack some scripts, execute them inside and in "pre-integration-test" phase, so it runs by default for all child modules.
My problem here is I need to delete the contents of certain directory each time it runs. I tried using ant-plugin which never runs in the pre-integration-phase. Also to note I'm calling several profiles while building the project.
mvn clean install -Pprofile1,profile2,integration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>pre-integration-test</phase>
<configuration>
<tasks>
<delete>
<fileset dir="checkout\myproject\specific_directory\**.*"/>
<delete/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
Overall I have four plugins including ant clean all running in pre-integration-phase. Except ant clean up task all others run correctly.

Based on the documentation and on my personal experience i assume you configured the plugin in the wrong area. Furthermore have you called mvn via:
mvn verify
to execute integration-test phase.
<build>
[...]
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>cleanup</id>
<phase>pre-integration-test</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>some/relative/path</directory>
<includes>
<include>**/*.tmp</include>
<include>**/*.log</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
<executions>
</plugin>
[...]
</build>

Related

Generated sources being compiled twice

Using Eclipse Luna with m2eclipse, I have a parent Maven project (facturas_root) and two Maven modules inheriting from it (sharepoint_ws and api_sharepoint).
sharepoint_ws was to be used only to generate JAXWS classes to connect to the Sharepoint WebServices, so I downloaded the related WSDL and included those as resources of the project. At generate-sources phase, it works correctly and generates the sources in target\generated-sources\ws-consume\mypackage\.
Now, the issue is that I made api_sharepoint import the sharepoint_ws dependency, but it does not detect any class. I assumed that it was because the generated classes were not at src/main/java, so I added a plugin to copy them there. Now, the problem is that at the compile phase of sharepoint_ws, it finds twice the source file of each class and throws an error.
My pom.xml -> build
<plugins>
<!-- clean /src/main/java and /target/generated-sources -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/src/main/java/es</directory>
</fileset>
<fileset>
<directory>${basedir}/target/generated-sources</directory>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
<!-- generate jaxws -->
<plugin>
<groupId>org.jboss.ws.plugins</groupId>
<artifactId>maven-jaxws-tools-plugin</artifactId>
<version>1.1.2.Final</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsconsume</goal>
</goals>
<configuration>
<wsdls>
<wsdl>${basedir}/resources/lists.wsdl</wsdl>
</wsdls>
<targetPackage>es.ssib.otic.facturas.sharepoint_ws</targetPackage>
<outputDirectory>${basedir}/target/generated-sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy sources -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/generated-sources/wsconsume</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In order to try to exclude target/generated-sources I have addded this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<configuration>
<excludes>
<exclude>**/target/generated-sources/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
As stated above, I do comment the "copy" plugin, the module depending on sharepoint_ws does not have any ot its classes available; I do use it I get errors in the tune of
[ERROR] /C:/Users/s004256/workspace/facturas_root/sharepoint_ws/src/main/java/es/ssib/otic/facturas/sharepoint_ws/DeleteList.java:[34,8] duplicate class: es.ssib.otic.facturas.sharepoint_ws.DeleteList
for each generated list.
In the first place, I recommend you'd better declare target/generated-sources as a source folder, instead of copying files here and there:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>initialize</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
This should be enough to make Maven compile the target/generated-sources/*.java and package them all in the library, and also for Eclipse to recognize target/generated-sources as a source directory (after you execute Maven/Update Project).
By the way: You should take care of binding the plugins to a phase in the correct order: If you bound all tasks to "generate-sources", you have no gurantee about in which order will they be executed. And the same goes for the "compile" phase: You have to set properly the source folders, with its inclusions and exclusions, before the compile phase.
Take a look a the Default Maven Lifecycle and try to chose different, sequential phases for your tasks.

How to resolve a Maven "The packaging for this project did not assign a file to the build artifact" error?

I’m using Maven 3.2.3. I used to be able to run “mvn clean install” on my WAR project and the WAR would get installed to my local repository. However, I recently added a configuration so that my WAR would be constructed in place (config is below). Now, when I run
mvn clean install
I get the error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.5.2:install (install) on project myproject: The packaging for this project did not assign a file to the build artifact -> [Help 1]
How do I fix this? I tried making the “install” goal of the maven-install-plugin the default, but that didn’t help. Below is my Maven configuration …
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>${project.basedir}/src/main/webapp/WEB-INF/classes</directory>
</fileset>
<fileset>
<directory>${project.basedir}/src/main/webapp/WEB-INF/lib</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<useCache>true</useCache>
<workDirectory>/tmp/${project.artifactId}/war/work</workDirectory>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>none</phase>
</execution>
<execution>
<id>war-inplace</id>
<phase>package</phase>
<goals>
<goal>inplace</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>default-install</id>
<phase>none</phase>
</execution>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
Adding the below rpm entry
%_rpmfilename noarch/%%{NAME}-%%{VERSION}-%%{RELEASE}.noarch.rpm
in each of the following files mentioned here solved the problem for me. (from 64 to no-arch).
vim /etc/rpm/macros
vim ~/.rpmmacros

Remove or delete resource files from target directory using pom file

I have two profiles in pom.xml, and I have some resource files which I have added into target resource directory: ${project.build.outputDirectory}/resources during execution of the first profile. What I need to do is remove those resource files during execution of the second profile.
Is there any way to remove or delete existing files from target directory?
I do agree with Matthew's observations, but I got the impression that the original poster was asking how to automate execution of clean during (normal) "execution" of a profile.
You can define a plugin execution for the Maven Clean Plugin. It is normally only bound to clean, but by defining a plugin execution you can bind clean:clean (that is the clean goal of the clean plugin) to whichever lifecycle phase you want. The documentation of the Maven Clean Plugin has an example of how to do this. The documentation also has an example of deleting additional files. Merged the two looks like this:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>some/relative/path</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
I got the solution..!!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.outputDirectory}/resources" includes="*.xml" />
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
for reference - http://maven.apache.org/guides/mini/guide-building-for-different-environments.html
mvn clean will remove the target directory (and therefore all the files in it). If you want to remove only certain files from the target directory, a combination of:
excludeDefaultDirectories to stop it from deleting the whole directory, and
filesets to tell it what to delete
ref: http://maven.apache.org/plugins/maven-clean-plugin/clean-mojo.html
Solution with Apache Maven AntRun Plugin 1.8:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
dir="${project.build.outputDirectory}/resources"
includeemptydirs="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
I needed only a couple of files deleted from the output directory, the following worked fine for me.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/appContextLocal.xml" />
<delete
file="${project.build.outputDirectory}/appContextServer.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
I also figured that you can run any ant commands here replace what ever task you need in between the <tasks> .... </tasks> and it will work.
List of ant tasks that you can perform are here
Ref: http://maven.apache.org/plugins/maven-antrun-plugin/usage.html
thanks to above answers. finally i came to something like:
if you want to just delete some directories in target folder, you have to create some construct like this.
this for instance deletes just all contents of folders:
target/unpack
gen-external-apklibs
excludeDefaultDirectories allows to not delete complete target folder.
i used it to clean up target folder before lint analysis.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>Deleting all unnecessary files before lint analysis</id>
<phase>verify</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>target/unpack</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
<fileset>
<directory>gen-external-apklibs</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
</filesets>
<verbose>true</verbose>
</configuration>
</plugin>

How do I include test classes and configuration in my war for integration testing using maven?

I currently have a maven web project that I am attempting to write integration tests for. For the structure of the project, I've defined test stubs under src/test/java, whilst the spring bean definitions for these stubs sit under src/test/resources.
What I would like to do, is that when I build my war artifact I'd like all of the test stub classes to be compiled and included in the war along with the spring bean definition files. I've tried to do it with the maven war plugin but the only things I've been able to copy are the resources. Simply put, I'd like to make use of the test class path and include all these classes in my war file.
It seems the useTestClassPath option with the maven jetty plugin would solve my problem but the current project I'm working on is currently using Tomcat 6.0. Is there another maven plugin or a way I can configure the maven war plugin to achieve my objective?
You can also do it straightforwardly. This will add both test classes and test resources to the WEB-INF/classes:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I also recommend you place it into separate profile like "integration" and also to override the package name in that profile to not be able to confuse normal war without tests packaged in and the testing war.
The full example with profile is here. You may run mvn clean package to have a war war-it-test.war without tests included, or you may run mvn clean package -Pintegration to have a war war-it-test-integration.war for the war with tests included.
I believe the following configuration for the maven war plugin would do what you want. You copy your test-classes to your WEB-INF/classes folder. You can even filter those resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>generate-test-war</id>
<phase>pre-integration-test</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
<configuration>
<warSourceDirectory>${basedir}/src/test/webapp</warSourceDirectory>
<warName>${project.artifactId}-test</warName>
<webappDirectory>${basedir}/target/${project.artifactId}-test</webappDirectory>
<primaryArtifact>false</primaryArtifact>
<webResources>
<resource>
<directory>${basedir}/target/test-classes</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
See http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
You can use the maven build helper plugin to add additional folders to the "normal" class path.
But I would recommend to create an new folder for your integration test (for example src/it/java), and add this folder, but not the "normal" test folder (src/test/java) -- the same for the resources folder.
Instead of using the maven antrun plugin, you could instead use the maven resources plugin and configure it like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<id>test-classes</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>false</overwrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/test-classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Use Tomcat 7 plugin with additional classpath directories configuration.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<additionalClasspathDirs>
<additionalClasspathDir>${build.testOutputDirectory}</additionalClasspathDir>
</additionalClasspathDirs>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
</executions>
</plugin>
You can do this Configuration in pom.xml file you don't get any errors in pom.xml and adding test classes to our jar or war file.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
We need to add the below plugin to the pom.xml in order to add the test cases to jar. Thanks to #IvonSopov
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
But after adding this line we got the build succeeded and also able to add the test case classes into the jar.. but the problem is in pom.xml it is showing as error like
Plugin execution not covered by lifecycle configuration:
org.apache.maven.plugins:maven-antrun-plugin:1.8:run
(execution:default, phase: process-test-classes)
In order to remove this error we need to include the below plugin as a separate tag within the build tag. (not inside the plugins which we added earlier.)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Now we can create the jar which includes the test classes without any errors.

How do I configure when cobertura tests run in maven-cobertura-plugin?

In order to fine-tune which tests are run at which times and in which environments, we have several executions set up for the maven-surefire-plugin. We set the default configuration to skip all tests, then enable them for the executions we want. This by itself works well for us.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTests.java</exclude>
</excludes>
</configuration>
<execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
</configuration>
<execution>
</executions>
</plugin>
When I add the maven-cobertura-plugin to the mix, I run into problems. The cobertura goal runs, and successfully instruments my classes. However, no tests get run. I assume this is because the test execution that cobertura is running in is one that is skipped. However, I cannot find how to specify which phase and goal to set up for this execution. When I turn on all tests, the output seems to indicate that these are still running in these unit-tests and integration-tests phases/goals.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
How do I need to specify a surefire execution so that the cobertura will run it against the instrumented classes?
You will note from the docs that cobertura:cobertura
Must be wired as a report
Instruments, tests and generates a report
Runs in its own lifecycle cobertura (not the default lifecycle)
Invokes lifecycle phase test before running itself
So, wiring it accordingly should automatically result in instrumentation and testing.

Resources