Maven - Have an XSD as a dependency - maven-2

We have one project that defines the message formats it produces with XSD files.
What is the easiest way to make these XSD files as dependencies of another project?
I was looking at using the maven-build-helper attach-artifact goal to attach my XSD files.
Is there a better mechanism?

I don't know the attach-artifact goal but I did something like you asked for. I had wsdl and xsd files to write Webservice artifacts and its client artifacts with axis2.
I put my wsdl and xsd in an own
project named 'wsdl' to
src/main/resources/META-INF and
nothing else.
I made a own project
named 'soap' for the generated
Java-SOAP-Code. In this project I
added the the wsdl project as
dependency and unpacked the wsdl and
xsd files via
maven-dependency-plugin to the
target folder in the
initialize-phase. So I can use it to
generate the SOAP-Code.
The soap
project I used as dependency for the
Webservice project and for the
client project.
I put all these projects to a multi module project so that I can build all together.
I think the important part for you is the configuration of dependency-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-wsdl-dependency</id>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${groupId}</groupId>
<artifactId>wsdl</artifactId>
<outputDirectory>target/wsdl</outputDirectory>
<includes>META-INF/*.wsdl,META-INF/*.xsd</includes>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
Hope that helps.
Greetings Michael

Related

Intellij path rule to automatically recognize generated sources folder

I would like to know if it is possible to set up a rule that marks a generated sources folder as generated sources root in Intellij Idea automatically.
Usually, Intellij detects the target/generated-sources directory as generated sources. My problem is that I also need it to automatically recognize the directory target/generated as generated sources, which Intellij never did in my case.
This is because of a maven plugin that I use for generating code from XSD schema:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${cxf.version}</version>
<configuration>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${cxf.version}</extension>
</extensions>
</configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
<configuration>
<xsdOptions>
<xsdOption>
<xsd>src/main/resources/schema.xsd</xsd>
<packagename>org.example.project.common.request</packagename>
</xsdOption>
</xsdOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
For some reason, this plugin generates code into the target/generated directory, and not into the target/generates-sources, of which I read that it is the convention and the default from many points of view.
I've tried searching on the web with similar keywords like in the title, but this was the closest solution to what I wanted to achieve. And even this solution doesn't solve my problem because Intellij doesn't allow setting some path patterns; it only offers a few options that don't include target/generated directory.
Another solution suggests changing the target output, which I can't do in every single project I work on; that is not a solution either.
This is important to me because I work with many projects, and sometimes when my code builds with maven but doesn't compile with Intellij I forget to check whether I marked all the generated folders as sources, or I don't even know there are generated sources in the project.
Does someone know a way I can achieve that Intellij automatically detects source files in target/generated directory?

jaxws-maven-plugin to create stubs and include wsdls in target location

During the development, I am facing some issues to generate stubs with maven jaxws-maven-plugin. My requirment is given below.
I have some wsdls in my specified location, now my jaxws-maven-plugin will read them from the specified location and will create the stubs files for me. In the In the stubs files wsdlLocation will be the name of the wsdl files exist on the each location and I will specify the location manually.
Secondly, maven will also copy the wsdl files in the location where classes are build. So, latter I can refer to those wsdls locally.
I have written my plugin where I am able to generate the stub classes but wsdl file locations not containing the location I want. Also need help to copy the wsdl files to the location I need.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>${project.build.directory}/wsdl/coh/BCS_COH-CXP9022427-${coh.version}/wsdl</wsdlDirectory>
<wsdlfiles>
<wsdlfile>\Service.wsdl</wsdlfile>
</wsdlfiles>
<wsdlLocation>/*</wsdlLocation>
<sourceDestDir>src/main/java</sourceDestDir>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
After several googling, I found the answer. Firstly I was using org.codehaus.mojo's jaxws-maven-plugin but above code will work if one use
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2.1</version>

maven: add arbitrary file as a servlet context resource

I have a maven war project which produces webapp.war, and a maven 'skin' project which produces skin.zip (a file full of resources and XML files). Now I want to add this zip file as a servlet context resource (e.g WEB-INF/skin.zip).
I tried using overlays, but it expands the zip file into WEB-INF instead of placing the un-expanded file there:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>com.mycompany</groupId>
<artifactId>skin</artifactId>
<type>zip</type>
<targetPath>WEB-INF</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
Is there any way to prevent it from expanding the resource -- or somehow stick the file in there (without using ant-plugin).
Note: type is a totally unnecessary and unhelpful configuration element -- it does not tell the plugin how to expand the artifact, as you might expect -- it tells it how to FIND it. For example if you change type from zip to jar, it complains that it can't find the artifact (in the most unhelpful way possible).
I tried using overlays, but it expands the zip file into WEB-INF
Yes, that's what overlays do, the content is unpacked to be merged with the war. That's just not the right tool in your case.
Is there any way to prevent it from expanding the resource -- or somehow stick the file in there
I would use the Maven Dependency Plugin and its dependency:copy goal:
dependency:copy takes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in local.
And bind it on the prepare-package phase. Below, some starting point:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-prepare-package</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mycompany</groupId><!-- or ${project.groupId} -->
<artifactId>skin</artifactId>
<version>X.Y.Z</version><!-- or ${project.version} -->
<type>zip</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
See Copying specific artifacts for more examples.

Maven pom.xml - Project Aggregation

We have a aggregation .pom set up to include several, individual modules, similar to the Maven documentation:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>my-module</module>
<module>my-module-2</module>
</modules>
</project>
Is there a way to get the artifacts from the builds (.JAR files) from these two modules into a common 'dist' directory after building? I did not want to adjust to output directory for the individual modules from "my-module/target" since they can be built separately as well.
I'm a Maven new-comer, so I'm sure there's an easy way to do this I'm missing.
Is there a way to get the artifacts from the builds (.JAR files) from these two modules into a common 'dist' directory after building?
The Maven Assembly Plugin can do that, it is very powerful and flexible. But power and flexibility also mean that this is not the most trivial plugin to use. In your case, the idea would be to generate a dir distribution from a moduleSets and you'll have to create a custom assembly descriptor for that.
I suggest starting with chapter 8.2. Assembly Basics of the Maven book and to pay a special attention to the chapter 8.5.5. moduleSets Sections.
After reading more at the links from the other answers, here is what I'm going to try for now:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-jars</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>../src/my-module/target</directory>
<includes>
<include>**/my-module*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Not exactly pretty, but while researching the Assembly plug-in for a possible longer term solution, this will do.
i guess maven assembly plugin can do this
As #Pangea said assembly plugin will do it. Just run assembly:assembly goal with appropriately set outputDirectory parameter.
more info at http://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html

I just need one dependent module, but don't need its dependencies

In my project , there is a spring XML config that utilizes ehcache to cache method returns.
xml snippet :
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
<ehcache:config configLocation="classpath:ehcache.xml"/>
<ehcache:proxy id="explainRetriever" refId="explainRetrieverImpl">
<ehcache:caching methodName="get*" cacheName="explainCache"/>
</ehcache:proxy>
but in runtime , server complains it cannot find definitions of http://www.springmodules.org/schema/ehcache , I know I have to add "spring-modules-cache.jar" to WEB-INF/lib directory.
But my project is maintained by maven , if I add "spring-modules-cache" to the runtime dependency , it will bring a lot of dependencies to my WAR file , filling my WAR with a lot of unnecessary jars. I just need one declaration in it , not all of its dependencies ...
Is there any way to tell maven not to include its dependencies to the project ?
Or ... another way , when packaging WAR , just copy another prepared spring-modules-cache.jar to WEB-INF/lib , how to do this ? Thanks !
As mentioned, you can use the exclusions mechanism, but that basically means excluding every runtime dependency declared in the dependency's pom, which is not only a pain, but fragile, since you have to keep tracking the dependency's pom to keep your list of exclusions up to date.
An alternative is to mark the dependency as <scope>provided</scope> and then copy the dependency yourself to the war construction directory under 'target' using the dependency:copy plugin goal. By default a war is built in
${project.build.directory}/${project.build.finalName}. See the war plugin for details.
E.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-cache</artifactId>
<version>0.8</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Have a look at this article - basically you need to use exclusions.