How to include automatically xmlbeans generated code into maven jar? - maven-2

I have a project which uses Apache Xmlbeans for databinding. Currently it is very simple it only has some Schema-Files in src/main/xsd and xsdconfig in src/main/xsdconfig.
I want to include the generated Classes into the generated jar-File. It works if I specify the xmlbeans goal:
"mvn xmlbeans:xmlbeans package" --> Creates a Jar with the xmlbeans classes
But I want to do this within the normal build cycle: "mvn package" --> should create a jar with the xmlbeans classes, but won't.
The pom is the following:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>xmlbeans-maven-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>maven-xmlbeans-plugin</artifactId>
<version>2.3.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I tried to bind it manually to the "generate-sources" (And to the "compile" phase, too) phase, but it does not work.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.leradon</groupId>
<artifactId>xmlbeans-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>maven-xmlbeans-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
How can I configure the plugin, so that when I run "mvn package" all the generated classes are packaged into the jar?
Greetings,
lerad

If you configure the plugin under pluginManagement, you still need to declare it under plugins. To simplify, I'm not using the pluginManagement in the pom.xml below:
<project>
...
<dependencies>
...
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
With this POM (and some XSD in src/main/xsd which is the default location), running mvn clean package just works (i.e. sources are generated from the XSD, compiled and packaged as part of the build).

Try this.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id />
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/xsd</schemaDirectory>
<staleFile>${project.build.directory}/generated-sources/xmlbeans/.staleFlag</staleFile>
<verbose>false</verbose>
<quiet>false</quiet>
<javaSource>1.6</javaSource>
</configuration>
</plugin>

Related

Run tests from maven zip assembly

I have finally succeeded in getting Maven to zip together a bunch of jars using an assembly file and install it to my local repository. That was difficult enough...
Now my goal is to configure another maven project so that when I do "mvn test", it will pull in that zip, unpack it, and run tests from the jars within that zip file. Does anyone know how to do this?
Here is the POM for the assembly project:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<name>RigFocusOnDataHub</name>
<version>12.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>RigFocusOnDH.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>zip</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is the POM for the second project. Unfortunately, instead of downloading the zip file for RigFocusOnDataHub, it just fetches the jars for all of RigFocusOnDataHub's dependencies from the local repo.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pason</groupId>
<artifactId>RigFocusDHSystemTest</artifactId>
<version>12.2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.pason</groupId>
<artifactId>MurphyRigFocus</artifactId>
<version>2.0.0-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<version>12.2.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.pason</groupId>
<artifactId>MurphyRigFocus</artifactId>
<version>2.0.0-SNAPSHOT</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<version>12.2.0-SNAPSHOT</version>
<type>zip</type>
<outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<testClassesDirectory>${project.build.directory}/tests/MurphyRigFocus</testClassesDirectory>
<reportsDirectory>${project.build.directory}/surefire-reports/MurphyRigFocus</reportsDirectory>
<includes>
<include>**/*IT.*</include>
</includes>
<argLine>-Djava.library.path=${basedir}/target/classes/</argLine>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
</plugins>
</build>
You would need to:
extract the jars from the zip - this is easy enough with maven-dependency-plugin
cut transitive dependencies so your jars don't end up in the path twice - you can do that at the source with maven-shade-plugin or in the test project itself with dependencies exclusions
add the jars to your test classpath, there are many ways to do that, I would try to use additional parameters in surefire configuration first

Unzip dependency in maven

I have the following dependency in maven
<dependency>
<groupId>org.hyperic</groupId>
<artifactId>sigar-dist</artifactId>
<version>1.6.5.132</version>
<type>zip</type>
</dependency>
This creates sigar-dist-1.6.5.132.zip in my repository.
I have seen this question here, however I still can't make it work.
How can I unzip the sigar-dist.zip and place the content in a directory in my project? What is the mvn call I have to do to make it work?
You can do it with dependencies:unpack-dependencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack-sigar</id>
<phase>package<!-- or any other valid maven phase --></phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.hyperic</includeGroupIds>
<includeArtifactIds>sigar-dist</includeArtifactIds>
<outputDirectory>
${project.build.directory}/wherever/you/want/it
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Reference:
Unpacking project dependencies
dependency:unpack-dependencies
just a follow up on answer from #Sean Patrick Floyd
this is my final pom.xml to download and unpack tomcat
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.koushik.javabrains</groupId>
<artifactId>tomcat</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>tomcat</name>
<properties>
<tomcat.version>8.0.27</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-tomcat</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.apache.tomcat</includeGroupIds>
<includeArtifactIds>tomcat</includeArtifactIds>
<outputDirectory>
${project.build.directory}
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat</artifactId>
<version>${tomcat.version}</version>
<type>zip</type>
</dependency>
</dependencies>
</project>

Change maven dependency for artifact using classifier

With the maven jar plugin I build two jar: bar-1.0.0.jar and bar-1.0.0-client.jar.
Actually in my POM I have the following dependency:
<dependency>
<groupId>de.app.test</groupId>
<artifactId>foo</artifactId>
<version>1.0.0</version>
</dependency>
This artifact exist also in two version bar-1.0.0.jar and bar-1.0.0-client.jar
I want to make bar-1.0.0-client.jar dependent of foo-1.0.0-client.jar and bar-1.0.0.jar dependent of foo-1.0.0.jar.
================
->First (wrong) solution: define the scope as provided and use the right foo package when using bar.jar
->Second (long) solution : Add 'server' classifier to the other jar. Use different profile to build the foo artifact and put the classifier in a property.
<dependency>
<groupId>de.app.test</groupId>
<artifactId>foo</artifactId>
<version>1.0.0</version>
<classifier>${profile.classifier}<classifier>
</dependency>
================
Concerning the profile solution.
Interfaces module pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.app</groupId>
<artifactId>myapp-parent</artifactId>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myapp Interfaces</name>
<profiles>
<profile>
<id>server</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>jar-server</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>server</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>client</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>jar-client</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<excludes>
<exclude>**/server/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Implementation module pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.app</groupId>
<artifactId>myapp-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>myapp-model</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myapp Model</name>
<properties>
<myapp-interfaces.classifier></myapp-interfaces.classifier>
<myapp-interfaces.version>1.1.0-SNAPSHOT</myapp-interfaces.version>
</properties>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>${myapp-interfaces.version}</version>
<classifier>${myapp-interfaces.classifier}</classifier>
</dependency>
[...]
</dependencies>
<profiles>
<profile>
<id>server</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>jar-server</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>server</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>${myapp-interfaces.version}</version>
<classifier>${myapp-interfaces.classifier}</classifier>
</dependency>
</dependencies>
<properties>
<myapp-interfaces.classifier>server</myapp-interfaces.classifier>
</properties>
</profile>
<profile>
<id>client</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>jar-client</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<excludes>
<exclude>**/server/**</exclude>
<exclude>**/META-INF/services/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<myapp-interfaces.classifier>client</myapp-interfaces.classifier>
</properties>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>${myapp-interfaces.version}</version>
<classifier>${myapp-interfaces.classifier}</classifier>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
The problem with this solution is due to the fact that my client interface have some missing interfaces and maven throw a compilation error during the compile phase.
And if I use myapp-model and an other project I didn't not have dependency to the right myapp-interface.
I wonder if it's possible to build a jar and put a specific pom inside ?
For the Interfaces.
I change nothing and build the both interfaces.jar (client + server).
For the Model
I import the both jar as optional
<dependency>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>${myapp-interfaces.version}</version>
<classifier>client</classifier>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.app</groupId>
<artifactId>myapp-interfaces</artifactId>
<version>${myapp-interfaces.version}</version>
<classifier>server</classifier>
<optional>true</optional>
</dependency>
With that I can build the both model's version without any error.
In my client application and server application
For each application I create the dependency to the right interfaces.jar and models.jar

Problems with maven output directory

I'm using almost same POM for both my projects, they are on the same workspace but they are not related at all, they are related however because in both I use spring and jboss. Here is the pom :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springinaction.hello</groupId>
<artifactId>spring-in-action</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-in-action</name>
<url>http://maven.apache.org</url>
<properties>
<jboss.ome>C:\jboss-5.1.0.GA\server\default\deploy</jboss.ome>
<springversion>2.5.3</springversion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<warName>spring-book</warName>
<outputDirectory>${jboss.ome}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
What I want to achieve with this POM is that I want to name my war when built spring-book.war and copy it to Jboss location I specified. Now in my first project this works it does exactly what I requested but in other it does not. I changed springversion and jboss home properties variable but everything remains the same, what can I do ? The project builds and all, everything is working perfectly just I don't want to copy everytime in my jboss dir and previously remove the old war, it takes about 20sec on each source code change its a lot
Problem spotted at this line:
<packaging>jar</packaging>
You're not using the right packaging, it should be:
<packaging>war</packaging>
After this change the war plugin should get called and things should work like in the other project :)
You could leave the output directory at its default, and use a profile instead with the maven jboss plugin. It has a hard-deploy target which copies your artifact to the deploy directory. If it's in a profile, you can activate it when (and only when) you want.
Moreover, with the antrun plugin, you can also delete the old war file before copying over the new one (this is useful when the war filename includes the version, but in your case may not be needed).
<profiles>
<profile>
<id>deploy</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>remove-old-war</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${jboss.ome}"
includes="*.war"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<executions>
<execution>
<id>redeploy-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You can then activate the profile with
mvn -Pdeploy install

How do I link a plugin execution to a phase in maven without forcing me to specify plugin on command line

I have a simple pom and added an ant-run to the compile but it only executes then when I do the following:
mvn install antrun:run
mvn install -- doesn't process the ant-run
mvn antrun:run -- doesn't process the ant-run
I thought that be linking the plugin to the lifecyce phase that the plugin would be executed when I try to achieve that phase. This is not what is happening.
Am I missing some nuance, do I need to have a profile which enables the plugin?
Thanks for the help (pom below)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>antecho</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Hello,world"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You've specified the plugin below the <pluginManagement> section. This means that this configuration will be used if the plugin is also declared directly under build/plugins, for example in a child POM.
To make it work in this instance remove the <pluginManagement> tags so that <plugins> is directly below <build> like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
...
</plugin>
</plugins>
</build>
You need to add a phase.
e.g.:
<executions>
<execution>
<id>xml2fastinfoset</id>
<phase>generate-sources</phase>
<goals>
<goal>xml2fastinfoset</goal>
</goals>
</execution>
</executions>
You might find this maven antrun example helpful.