Maven - Copy Some Dependency JARs into warSourceDirectory - maven

Can any Maven plugins copy one or more dependencies (although not all of them) of a .war project into its warSourceDirectory (src/main/webapp)?
I'm working on a Java web-app that will display an applet. I'd like the war project to pick the latest version of some jars (the applet's dependencies) and stick them in /src/main/webapp/, to save me having to copy them around the place.
I've thought about shading and uber-jar-ing the applet itself, but I'd like to split the jars up to save users having to download one massive jar when only one of the bits in it has changed.

Use for this dependency:copy from Maven Dependency Plugin.
Read also example page: Copying specific artifacts
Sample configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
Copying files into src/main/webapp is bad idea. Copy file directly into target folder.

Related

Maven include zip dependency

We have an zip artifact hosted on our local repository. Within my Maven project I would like to retrieve this zip dependency.
From command line I can do that via:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
-DrepoUrl=https://artifactory.company.net/artifactory/releases\
-Dartifact=com.company.api:my-swagger-doc:$VERSION:zip:resources
This will download the my-swagger-doc-2.5.3-resources.zip. I don't know how to do this in XML in my pom.xml.
My goal is to generate Spring controllers from this swagger file.
My question is:
How can I download this zip artifact and let it extract in my target directory?
You can use unpack goal of Maven Dependency Plugin.
Find below an example snippet:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<type>zip</type>
<outputDirectory>target/</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
In my example, I have attached the execution to generate-resources phase. Depending on your build, you may have to attach the execution of this goal to the appropriate phase.

Unzipping downloaded artifacts + dependency list

We use maven-dependency-plugin to download ZIP artifacts from our repository in Nexus and then unzip them. The corresponding command in pom.xml is like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-zip-artifacts</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.ourdomain</groupId>
<artifactId>our-item</artifactId>
<version>1.0.0</version>
<classifier>bin</classifier>
<type>zip</type>
<outputDirectory>./ourfolder</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
Everything works perfectly.
Nevertheless, we'missing one thing: when we create a site for the artifact (command mvn site) the downloaded and unzipped artifacts don't appear under Dependencies because the corresponding entries in pom.xml's <dependencies> section are missing. Of course we can make the entries but they are REDUNDANT to those made above. Having dozens of artifacts it's pretty lot.
Is there any possibility to solve this problem in an 'elegant' way?

how to run maven war dependency using tomcat:run?

I am using maven for my current projects. Now, I have a war dependency:
<dependency>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo-war</artifactId>
<version>1.8.1</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
I don't have any problem when I built the war artifact. For this, I have added this plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo-war</artifactId>
<targetPath>js/dojo-1.8.1</targetPath>
<excludes>
<exclude>WEB-INF/**</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
But, when I run the following mvn command mvn tomcat7:run I can not see any javascript resource. It looks like war dependency had not been added.
Could someone help me with that?
Thanks in advance.
Welcome to Stack Overflow Emelendez.
You should read this post : http://webtide.intalio.com/2008/07/dojo-toolkit-maven-repository/
Indeed, the dojo-war dependencies allows you to add some files to your sources, but you must specify some extra actions to include it in the final war.
Dependencies, in Maven, can only be in the language you try to compile (well it's not really exact, but this is enough here). If you want to add other resources (files, images, javascript), contained in a zip/war/targz, you must explicitely extract them. This is what is mentionned in the link I provided previously :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack dojo</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo</artifactId>
<version>${project.version}</version>
<type>zip</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/dojo</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
(Be careful, I'm not sure that this configuration is exact, your dependencie seems to be dojo-war according to your pom.xml, and not dojo as per doc said)
This will extract dojo libs into output directory, and then into your war.

copy dependency assembly resources to specific folder

I have ProjectA which uses the Maven assembly plugin to pack some resources from a module into the repository.
I have then ProjectB which has a dependency on ProjectA. In ProjectB, I'd like to use the maven-dependency-plugin to unpack the module resources(packed by the assembly plugin) into some target folder of my choice.
I configured the dependency plugin as following, but when I run maven, it will only copy the module's resources and not also the assembly resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
You would need to specify the correct classifier and type to maven so that it can do this. Your assembly should available in your local repository or downloadable from repository.
For instance, assuming your assembly was named moduleA-distribution-1.0.zip, you would alter the above snippet as follows:
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<classifier>distribution</classifier>
<type>zip</type>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>

Maven and copying at test time

How can I have maven copy some resource files from another project right before the junit test cases are run?
I want this because the json files are maintained by the ui team for their testing purposes, so I want to use those same files to run against our layers so that we know we don't break the expected output when we change our code.
One option is to use the maven dependency plugin to selectively "unpack" the files from the project which has those files.
For example say the project in which the resource file is com.foo.bar:myArtifact:jar, so you can do something like
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>unpack</id>
<!-- You could a phase of your choice based on needs -->
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.foo.bar</groupId>
<artifactId>myArtifact</artifactId>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
<includes>*.json</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This will copy all .json files (just an example) to the current project's build output directory.
You can refer to the dependency plugin for more configuration options.
Hope this helps.

Resources