Gradle equivalent for mavens maven-dependency-plugin - maven

I'm moving from Maven to Gradle,
In my maven code i used the "maven-dependency-plugin"
and i could not find an easy translation for the following:
so my question is how can i get my dependencies into a specific structure?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1</version>
<type>swf</type>
<outputDirectory>/flex/output1</outputDirectory>
<destFileName>artifact1.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group2</groupId>
<artifactId>artifact2</artifactId>
<version>2</version>
<type>swf</type>
<outputDirectory>/flex/output2</outputDirectory>
<destFileName>artifact2.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group3</groupId>
<artifactId>artifact3</artifactId>
<version>3</version>
<type>swf</type>
<outputDirectory>/flex/output3</outputDirectory>
<destFileName>artifact3.swf</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>

so finding no equivalent in Gradle what i ended up doing is holding a map of artifact id to the new name (outputDirectory + destFileName) and after resolving the dependencies into a new special configuration which i called "Flex" and then renaming every dependencies from "flex" using the mapping.

For those looking for Maven-like dependency management features in Gradle, check out the Dependency Management Plugin. It provides familiar dependencyManagement blocks and BOM support.
Note that you don't need to use Spring in your project to use the plugin.

Related

Download war file from nexus with maven

If I have the artifactId, the groupId and the version how I can configure the pom.xml so maven will download test.war file from the nexus repository.
I assume that this should happened in the
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
.....
</plugin>
In the execution block.
I already tried this but no luck:
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<artifactId>mytest</artifactId>
<groupId>com.mytest</groupId>
<version>${version}</version>
<type>war</type>
<destFileName>mytest-${version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
Are you certain that ${version} is being set properly for you? If you need the version of the current project, you should probably be using <version>${project.version}</version> and possibly setting up <dependencyManagement/> to handle that for you.

Maven extract Dependency without creating dependency directory

The configuration below works but I end up with an extra directory that I don't want. So I have target/webapp/dep-A/<depedency contents>, what do i need to change to get it to be target/webapp/<depedency contents>?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>myGroup</groupId>
<artifactId>dep-A</artifactId>
<version>1.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/webapp</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
Unfortunately the maven-dependency-plugin unpack goal has not a postprocess optional parameter letting you, for example, chmod-ing or moving files. So I don't think exists an elegant way to do what you want only using the maven-dependency-plugin.
In addition to the maven-dependency-plugin you might use the maven-antrun-plugin, binding it to a subsequent phase which the maven-dependency-plugin is binded to, to copy/move/delete your files and directories.

How to add resources of an external jar into my maven build jar?

I have a module , while building this module i want to include some resources from an external jar into my module jar.
I tried to use maven-shade-plugin but I am not able to achieve it it.
Any help is appreciated.
Unpack your external resource into project target, after this unpacked resources will be included in generated artifact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<type>jar</type>
<includes>*.xsb, *.properties</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
For non-maven file use method from this answer https://stackoverflow.com/a/3264160/516167

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>

How to ignore maven-dependency-plugin copy goal errors?

I am copying a resource into another folder before packaging using the maven-dependecy-plugins copy goal.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<failBuild>false</failBuild>
<artifactItems>
<artifactItem>
<groupId>my.groupID</groupId>
<artifactId>myArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>my/custom/path</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
The resource is not vital and it is likely possible that it is not accessible. That's why I want the build not to fail if it is not accessible. I already set the failBuild property to false but it had no effect. Is there a way to achieve this?
I guess you get a resolve problem, that the artifact doesnt exist or can't be found in any repository. That's how maven works, if you specify a dependency you need to be able to retrieve it.
I resolved my problem by using the copy artifact plugin of jenkins now.

Resources