How to generate a WAR with the source code in Maven? - maven-2

I want to distribute the war of my web application generated with Maven with the source code inside it. How to do that with Maven?

It is possible configure the maven-war-plugin to include the source directory as it was a web resource:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${build.sourceDirectory}</directory>
<targetPath>sources</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The java sources will be included in a sources directory in the war. Of course you should adapt the resource directory to your own maven layout.

If you want the source files in the same directory as the class files you would use:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${build.sourceDirectory}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

Usually I think you would go this way: (this won't include the source files, but provides them as separate files)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

At your war project's pom.xml:
<build>
...
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
</build>
In the projects you want do use it:
<dependency>
<groupId>my-war-group</groupId>
<artifactId>my-war-artifact-id</artifactId>
<version>my-war-version</version>
<classifier>classes</classifier> <!-- THIS IS THE IMPORTANT LINE! -->
</dependency>

Related

How to generate TestNG runner file using cucumber-jvm-parallel-plugin?

Below is my POM file. I am unable to generate TestNG runner file while all the configs are correct. Please help me to do the correct configuration.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>4.2.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>generate-test-sources</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<outputDirectory>runner</outputDirectory>
<glue>
<package>com.compareglobalgroup.stepdefs</package>
<package>com.compareglobalgroup.cucumber.hooks</package>
</glue>
<featuresDirectory>src/test/resources/feature</featuresDirectory>
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<format>json</format>
<plugins>
<plugin>
<name>json</name>
</plugin>
</plugins>
<tags>
<tag>#BBDK</tag>
</tags>
<useTestNG>true</useTestNG>
<!-- <namingScheme>simple</namingScheme> <namingPattern>Parallel{c}TestRunner</namingPattern> -->
<parallelScheme>FEATURE</parallelScheme>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<forkCount>10</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/Parallel*IT.class</include>
</includes>
<!-- You can specify a specific testng.xml file here <suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng-sample.xml</suiteXmlFile> </suiteXmlFiles> -->
<!-- Or dynamically with something like '-DsuiteXmlFile=src/test/resources/testng-sample.xml' -->
<!-- <suiteXmlFiles> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> -->
<!-- Build with '-DskipTests=true' to bypass test execution # build
time Default: false -->
<skipTests>false</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<systemPropertyVariables>
<browser>${browser}</browser>
<execution>${execution}</execution>
<environment>${environment}</environment>
<!-- <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> -->
<country>${country}</country>
<vertical>${vertical}</vertical>
<homeUrl>${homeUrl}</homeUrl>
<isHeadless>${isHeadless}</isHeadless>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
You need to have your <plugin> entry available under
<build>
<plugins>
<plugin>
<!-- add your plugin entry here -->
</plugin>
</plugins>
</build>
But you have added it under <pluginManagement> which is usually used to control plugin behavior for projects that inherit from the current one. See here for more information.

Filter maven resources with filter-file from external dependency

Given a property file in maven project A
I want to use them in project B for resource filtering.
So in Project B I use
<build>
<filters>
<filter>${project.build.directory}/myFile.properties</filter>
</filters>
</build>
To filter my resources based on values in myFile.properties This file is stored in project A. So I include it with
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupa</groupId>
<artifactId>a</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Problem is that the resource filtering happens before the dependency is copied. So filtering does work when copying myFile.properties manually to ${project.build.directory} but it does not work with a mvn clean ...
How can I copy the filterFile before the actual filtering happens?
Maven plugins are executed in the order they appear in the pom. AFAIK the plugins configured in the parent (and the super pom) are executed before the plugins of the pom.
My suggestion is declaring the resources plugin explicitely after the dependency plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- ... -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!-- ... -->
</plugin>
</plugins>
</build>

Not able to generate client code with wsdl2java and maven using CXF

I'm using maven cxf-codegen-plugin to generate client files from wsdl but not able to do so.
I want that all the wsdl files in the folder src/main/wsdl should be scanned and corresponding clients should be generated in separate folders. Please help.
My pom.xml is :
<build>
<finalName>someFileName</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.2.3</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>process-resources</phase>
<configuration>
<sourceRoot>src/main/java</sourceRoot>
<wsdlRoot>${basedir}/src/main/wsdl</wsdlRoot>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
here's how I'm doing it with version 2.7.4, and having the generated code created in different packages :
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/MyWsdl1.wsdl</wsdl>
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-p</extraarg>
<extraarg>urn:mycompany:myproduct1:v1_0=com.my.project.product1</extraarg>
<extraarg>-p</extraarg>
<extraarg>http://www.w3.org/2001/XMLSchema=com.my.project.common</extraarg>
</extraargs>
</wsdlOption>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/MyWsdl2.wsdl</wsdl>
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-p</extraarg>
<extraarg>urn:mycompany:myproduct2:v1_0=com.my.project.product2</extraarg>
<extraarg>-p</extraarg>
<extraarg>http://www.w3.org/2001/XMLSchema=com.my.project.common</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Here's where you can find out more about the extra-args :
http://cxf.apache.org/docs/wsdl-to-java.html
For an automatic scan of the wsdl folder, this works good too :
<configuration>
<sourceRoot>${project.build.directory}/generated/src/main/java</sourceRoot>
<wsdlRoot>${basedir}/src/main/wsdl</wsdlRoot>
<includes>
<include>**/*.wsdl</include>
</includes>
</configuration>
Hope it helps!
I realize this is an old question, but I just ran into this, so I wanted to reply for the benefit of others. You are right on commenting out the <pluginManagement> tag see here. However for the error in Eclipse that says:
Plugin execution not covered by lifecycle configuration
You will need to install the m2e connector for build-helper-maven-plugin (click on the error, and Eclipse should guide you to install it)
I put plugins tag inside pluginManagement tag and error disappeared:
<pluginManagement>
<plugins>
<plugin>
..........................
</plugin>
</plugins>
</pluginManagement>

Share test resources between maven projects

There is a clear solution for sharing the common test code between maven projects using test-jar goal of maven-jar-plugin plugin (see here).
I need to do the similar thing with test resources, in particular, I want test resources of project A be available in the classpath of project B during testing.
For project A one need to declare:
<!-- Package and attach test resources to the list of artifacts: -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<jar destfile="${project.build.directory}/test-resources.jar">
<fileset dir="${project.basedir}/test-resources" />
</jar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/test-resources.jar</file>
<type>jar</type>
<classifier>test-resources</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
And in project B it will be normal dependency:
<dependency>
<groupId>myproject.groupId</groupId>
<artifactId>myartifact</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>test-resources</classifier>
<scope>test</scope>
</dependency>
Question: Should it work in all cases? Is it possible to pack resources without maven-antrun-plugin (using more 'lightweight' plugin)?
Just use jar:test-jar and declare the resulting JAR as a dependency (refer to this guide for more details). And while I don't understand the problem of having resources and classes in this jar, you can always exclude all .class files:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
And to use it:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
Accepted answer helped me, but it's not quite accurate in case you need regular jar of same project as well. It will delete *.class files from both jars.
Settings below translates to something like:
create me 2 jars: 1 regular, 1 test;
remove *.class files, but only from test jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Using maven-dependency-plugin we can put the resource needed in the right directory, only modifying the pom on dependent project is needed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-test-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>dependeeGroupId</groupId>
<artifactId>dependeeArtifactId</artifactId>
<version>dependeeVersion</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
<includes>resourceNeeded.txt</includes>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
type is used to get test resource
outputDirectory is used to put the resource usable in tests
Documentation here: https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html
There is already a goal to build a test jar from maven.
Assuming you need something a little more flexible, you can use the jar plugin to package your test resources and run that goal with the main package goal with something like this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>test-resources</classifier>
<includes>
<include>**/*.whatever-you-want</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Whatever you want bundled would be added to the project-name-version-test-resources.jar when the jar goal is run.
You could then include it in a project via the same dependency you use above.

Maven WAR plugin not reading configuration when running in <execution> tag

I'm trying to get Maven to perform several executions with the WAR plugin. It works fine as long as it's defined in the following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>1.0</version>
<configuration>
(...)
</configuration>
But not in the following manner
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
(...)
</configuration>
</execution>
</executions>
</plugin>
Where Maven can't find any of the resources I defined in the <configuration> tag. Have I missed anything important, and/or is there a better way of constructing multiple WAR files in a single build?
I didn't see how to turn off the war that's generated by default, but you can use one configuration outside the <executions> element and the rest inside:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<classifier>with-junk</classifier>
<!-- temp directory that the webapp is assembled in (each must be different) -->
<webappDirectory>${project.build.directory}/build-with-junk</webappDirectory>
<webResources>
<resource>
<directory>junk</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<id>add-other-junk</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<!-- exclude prior configuration -->
<inherited>false</inherited>
<configuration>
<classifier>with-other-junk</classifier>
<webappDirectory>${project.build.directory}/build-other-junk</webappDirectory>
<webResources>
<resource>
<directory>other-junk</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</execution>
</executions>
</plugin>
For me, this builds artifact-0.1-with-junk.war and artifact-0.1-with-other-junk.war and both have the correct files included.
The second version applies the configuration only to the phase you've specified. I'm not able to confirm this right now, but I'd guess it is not being applied because you haven't specified a goal for the configuration to be applied to.
If you add the war goal definition into the execution does it get applied? Like so:
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
(...)
</configuration>
</execution>
</executions>