Install this theme
Using XSDs or any other custom resources as Maven dependencies

Often it is required to put some custom resources (e.g. XSDs, WSDLs etc.) under dependency management control.

As soon as Maven is a de facto standard dependency management mechanism in the Java world, this post demonstrates how to use it to manage untypical custom resources as dependencies within the pom.xml.

Let’s assume we have some xsd file (test.xsd in this example), that we want to use as dependency in our POM.

First, we’ll deploy it to the local repository (alternatively you can deploy it to your company’s internal maven repository):

mvn install:install-file -Dfile=test.xsd -DgroupId=com.mycompany.app \
           -DartifactId=app-xsd -Dversion=1.0.0 -Dtype=xsd -Dpackaging=xsd

After installation you’ll find the xsd in your local maven repository within the following folder structure:

image

Next referencing this file as a dependency in your POM is quite simple. Just write:

<dependency>
	<groupId>com.mycompany.app</groupId>
	<artifactId>app-xsd</artifactId>
	<version>1.0.0</version>
	<type>xsd</type>
</dependency>	 

Furthermore if you want to add this file as a resource to the final artifact generated by Maven, you have to configure maven-dependency-plugin in your POM like this:

<build>
	<plugins>
		...
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<version>2.5.1</version>
			<executions>
				<execution>
					<id>copy-dependencies</id>
					<phase>generate-resources</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
						<outputDirectory>${project.build.directory}/${project.build.finalName}/META-INF</outputDirectory>
						<includeArtifactIds>app-xsd</includeArtifactIds>
						<includeTypes>xsd</includeTypes>
					</configuration>
				</execution>
			</executions>
		</plugin>
		...
	</plugins>		
</build> 

After executing the maven build the app-xsd-1.0.0.xsd file will land in the artifact’s META-INF folder.

 
Blog comments powered by Disqus