Maven dependency plugin - exclude directories when unpackaging jar file - java

I am trying to un-package a jar file using the maven dependency plugin. But I only want one file inside the jar file and want to exclude the META-INF directory that's inside the jar. How would I do this?
This is what I have so far:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<type>jar</type>
<outputDirectory>${project.basedir}/src/test/</outputDirectory>
<excludes>META-INF</excludes>
</artifactItem>
</artifactItems>
<excludes>META-INF</excludes>
</configuration>
</execution>
</executions>
</plugin>

Found the solution.
<artifactItem>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<type>jar</type>
<outputDirectory>${project.basedir}/src/test/</outputDirectory>
<excludes>META-INF/</excludes>
</artifactItem>
Just add a forward slash: / after the directory name.

Related

Download specific jars with dependency using maven plugin [duplicate]

I have a maven project which I have say spring framework libraries as dependencies, I want to copy spring framework dependencies with there transitive dependencies to a location specified.
I have gone through maven dependency plugin guides at apache, I have several options where non of them will solve the complete problem.
copy dependencies option
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
This will copy all the dependencies and there transitives to a given location, I want only spring dependencies and there transitives.
copying specific artifacts
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
<type>jar</type>
<overWrite>false</overWrite> <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
This is not coping the transitive dependencies.
Any solution which solve my both problems.
This is possible with the assembly plugin.
Plugin configuration:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
<finalName>plugins</finalName> <!--folder name in target directory-->
</configuration>
<executions>
<execution>
<id>some-id</id> <!-- must match assembly id in assembly.xml-->
<phase>pre-integration-test</phase> <!-- pic -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>some-id</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>
org.springframework:spring-web
</include>
</includes>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
The important bits are <useTransitiveDependencies>true</useTransitiveDependencies> and <useTransitiveFiltering>true</useTransitiveFiltering>, which cause the include to be applied to project dependencies, but not to transitive dependencies, resulting in spring-web artifact and it's dependencies to be copied to the directory.
You can use the maven assembly plugin for this.
Check it out and specifically the dependency set:
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet
You can provide an output directory and you can specify which dependencies to put in there
There is also an option: useTransitiveDependencies. Set this to true to get the behaviour you want.
Here's an option:
create module (producer) to collect dependencies and publish them as a zip.
in consumer user depencency:unpack to unpack that zip
It is cumbersome and the exclusions still need some cherry picking, but much less and it could be run in parallel threads.
Producer
<project 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>packaging</groupId>
<artifactId>jdbcdrivers</artifactId>
<packaging>zip</packaging>
<name>jdbcdrivers</name>
<dependencies>
<!-- set up deps for capture and limit their impact on anything which depends upon this module via optional-false -->
<dependency>
<groupId>net.sf.jtds</groupId>
<artifactId>jtds</artifactId>
<scope>test</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<scope>test</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
<optional>false</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>dist profile: hive jdbc driver ${baseName}</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/lib/addons/jdbcdriver/</outputDirectory>
<useBaseVersion>true</useBaseVersion>
<excludeTransitive>false</excludeTransitive>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>test</includeScope>
<excludeScope>provided</excludeScope>
<excludeGroupIds>org.codehaus.groovy,org.apache.ivy,jdk.tools</excludeGroupIds> <!-- you might need to cherry pick excludes if scope doesn't worjk -->
<prependGroupId>true</prependGroupId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Get JRE from maven

Is it still possible to download JRE from maven as a zip file, so that one can include it in the packaged product? I found this code, which doesn't work anymore:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.oracle</groupId>
<artifactId>jre</artifactId>
<version>1.8.141</version>
<type>zip</type>
<classifier>windows-i586</classifier>
<outputDirectory>${basedir}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
UPDATE: Looks like such thing could only work after uploading the zip file to the own maven repo...
Change the version 1.8.141 to 1.8.0_131. The latest maven has this one only:
<!-- https://mvnrepository.com/artifact/com.oracle.java/jre -->
<dependency>
<groupId>com.oracle.java</groupId>
<artifactId>jre</artifactId>
<version>1.8.0_131</version>
</dependency>
EDIT :As per the comments from OP
This example here is for a dependency. What I need is a maven goal to
copy unzipped JRE to a folder. And actually I do need a specific JRE
version. So currently, the solution is to install the JRE zip file in
my maven repo and unpack it with maven goal.
Copying and unzipping the jre artifact to another location may be achieved by Maven Dependency Plugin
<project>
[...]
<dependencies>
<dependency>
<groupId>com.oracle.java</groupId>
<artifactId>jre</artifactId>
<version>1.8.0_131</version>
</dependency>
</dependencies>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>validate</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.oracle.java</groupId>
<artifactId>jre</artifactId>
<type>zip</type>
<outputDirectory>/path/to/alternateLocation</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>

How to copy jar-with-dependencies in my webapp\myfolder in maven Idea

I have the maven project in Idea with two modules: 1)web and 2)web-start-swt-jar.
The web-start-swt-jar pom.xml is configured to package with-dependencies.
My project looks like this:
[root]
- web-start-swt-jar pom.xml (packaging jar)
- web pom.xml (packaging war)
pom.xml (packaging pom)
Is it possible to make theese steps I wanted to:
Compile,pacakge with dependensies the web-start-swt-jar.
Compile, package the web module with copying the recently packaged web-start-swt-jar to webapp\webstartFolder\ ?
Yes I use maven assemply plugin, but it copy the jar file wich without dependencies.
Thank you!
p.s. May be you suggest to use some plugins to comfort work with web-start? e.g. webstart-maven-plugin ? please help.
[root]
- web-start-swt-jar pom.xml (packaging jar)
- web pom.xml (packaging war)
pom.xml (packaging pom)
Try to add this : web pom.xml
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>web-start-swt-jar</artifactId>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>web-start-swt-jar</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<!-- outputDirectory not sure. to test-->
<outputDirectory>${basedir}/src/main/webapp/webstartFolder</outputDirectory>
<destFileName>web-start-swt-jar</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
It seems to be answer was found: using maven-shade-plugin to package jar with dependensies and maven-dependency-plugin to copy to custom folder in prepare-package phase.
Dont forget to set version of depended jar via <dependency managament>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>my.group</groupId>
<artifactId>webstart-swt-jar</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>
${project.build.directory}/${project.build.finalName}/SomeFolderHere
</outputDirectory>
<destFileName>(optional rename..)webstart-swt-jar.jar</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
ADDED:
But if I dont set webstart-swt-jar to dependency, build failed with error "webstart-swt-jar not found". Its not good, because artifact is duplicated in WEB-INF\lib and my \custom folder...

In maven, add a folder as a source folder but dont include it in source jar

I have a use case where I need to include a folder target/schemas as a source folder using maven-buildHelper plugin. I am generating a war for this project. When the source jar get created, the content of target/schemas also exists in that. I dont want add the content of target/schemas into my source jar. How can it be achieved ?
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>***************</groupId>
<artifactId>core</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>identity</artifactId>
<packaging>war</packaging>
<dependencies>
...............
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>identity</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>***************</groupId>
<artifactId>authentication-service</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
<type>jar</type>
<outputDirectory>${project.build.directory}/schemas</outputDirectory>
<includes>**/*.java,**/*.xml</includes>
</artifactItem>
<artifactItem>
<groupId>***************</groupId>
<artifactId>authorization-service</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
<type>jar</type>
<outputDirectory>${project.build.directory}/schemas</outputDirectory>
<includes>**/*.java,**/*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.plugin.buildHelper}</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/schemas</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>${maven.plugin.jaxb2}</version>
<executions>
<execution>
<id>schemagen-combined</id>
<goals>
<goal>schemagen</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<includes>
<include>***************/core/identity/domain/*.java</include>
<include>***************/authentication/domain/*.java</include>
<include>***************/authorization/domain/*.java</include>
</includes>
<outputDirectory>${project.build.directory}/schemas</outputDirectory>
<generateDirectory>${project.build.directory}/generated-sources</generateDirectory>
</configuration>
</execution>
</executions>
</plugin
</plugins>
I have a web project identity which depends on two jar authentication and authorization. In project identity, I am generating xsd using jaxb2-maven-plugin for authentication, authorization and identity sources. As the plugin jaxb2-maven-plugin, works only on sources exists in the maven source path, I am downloading sources for authentication and authorization in a folder target/schema, add this folder target/schema as a maven source folder and then running jaxb2-maven-plugin to generate xsd. Now, when I build source jar for identity, it also includes sources for authentication and authorization which I dont want.
Maven Source plugin has option for excluding selected files from created source JAR.
exclude
List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents is being packaged into the JAR.

retrieve Artifact item as a property

I'm trying to retrieve artifact items in maven-dependecy-plugin by name as a property to write its value in my war manifest file .
I need something like ${project.artifactItems["name"].value} to retrieve my values without having to read each jar manifest file as a stream programmaticaly.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-installed</id>
<phase>test</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>prj-applet</artifactId>
<version>${test.project.version}</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>prj-pdf-render-applet</artifactId>
<classifier>jar-with-dependencies</classifier>
<version>${test.project.version}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
I need to insert each retrieved value in my manifest file using the war plugin :
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>test</warName>
<!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
<!-- <warName>test</warName> -->
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifestEntries>
<applicationVersion>${pom.version}</applicationVersion>
<applicationBuildTimestamp>${maven.build.timestamp}</applicationBuildTimestamp>
</manifestEntries>
</archive>
</configuration>
suppose you have this dependency
<dependency>
<groupId>some-groupid</groupId>
<artifactId>some-artifactid</artifactId>
<type>jar</type>
</dependency>
you can get your dependency with ${maven.dependency.some-groupid.some-artifactid.jar.path}

Categories

Resources