Is it possible to create a POM that, on package or greater, merely assembles the entire project (for example, into a zip file) and places it in target?
In this case, the project does not have any Java code in it, it is merely a set of scripts and files that I would like to have packaged. For the sake of uniformity (because our shop is all Maven), I would really like to have a POM do this, as, currently, we have a shell script doing it.
Examples would be MUCH appreciated.
Thanks
So I ended up with the following, which creates a file ServerSetupTools-0.1-SNAPSHOT.tar.gz in target and that works for me. The only downside is that I wasn't sure how to get it to pull the files when they were in the root directory, so I moved them all to src/main/resources, which also worked for me. Hopefully this helps somebody else.
POM FILE:
<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>
<artifactId>ServerSetupTools</artifactId>
<packaging>pom</packaging>
<name>ServerSetupTools</name>
<url>http://maven.apache.org</url>
<groupId>com.mycompany.utilities</groupId>
<version>0.1-SNAPSHOT</version>
<build>
<plugins>
<!-- Run assembly as part of packaging -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>
src/main/assembly/assemble.xml
</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase><!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
src/main/assembly/assemble.xml:
<assembly xmlns="http://maven.apache.org/xsd/assembly"
xsi:schemaLocation="http://maven.apache.org/xsd/assembly-1.0.0.xsd">
<id>dist</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<includes>
<include>*</include>
</includes>
<directory>src/main/resources</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
Related
I want to have parent POM similar to the following:
<project>
<groupId>com.mycompany.whatever</groupId>
<artifactId>whatever-parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
<build>
<plugins>
<!-- plugin for generating resource files -->
<plugin>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<configuration>
<files>
<file>
<!-- location relative to jar's root -->
<location>lorem.txt</location>
<!-- file contents -->
<contents>Lorem ipsum dolor</contents>
</file>
</files>
</configuration>
<executions>
<execution>
<goals>
<goal>...</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
So when it will be used as parent POM then lorem.txt file will be automatically generated and packaged inside jar.
If you need a file include inside your jar/zip file.
Please refer the way to use maven assembly plugin:
http://maven.apache.org/plugins-archives/maven-assembly-plugin-2.6/examples/index.html
http://maven.apache.org/plugins/maven-assembly-plugin/
EX:
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/config_templates</directory>
<lineEnding>unix</lineEnding>
<outputDirectory>config_templates</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>dep/*</include>
<include>classpaths/*</include>
<include>endorsed/*</include>
<include>*.jar</include>
</includes>
<outputDirectory>target</outputDirectory>
</fileSet>
If you need the structure of your project from the first time, please refer the way to create proj from your own archetype:
https://maven.apache.org/archetype/maven-archetype-plugin/usage.html
I hope they can help you.
I'm currently having my JAVA Maven projects and trying to migrate it to Gradle. I issued the below command to convert it,
gradle init
Kindly find the pom.xml, the descriptor xml file of my project and the generated build.gradle files below,
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ProjectA</artifactId>
<packaging>jar</packaging>
<name>ProjectA</name>
<dependencies>
<dependency>
<groupId>org.jpos</groupId>
<artifactId>jpos</artifactId>
<version>113</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/non-distributable-lib/jpos113.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<archive>
<manifest>
<mainClass>com.test.jpos.Main</mainClass>
</manifest>
</archive>
<descriptor>src/main/assembly/assemble_POSMClient_JPOS_Bridge.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.test</groupId>
<artifactId>ProjectRoot</artifactId>
<version>5.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
build.gradle :
description = 'ProjectA'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile files('src/main/non-distributable-lib/jpos113.jar')
}
assemble_POSMClient_JPOS_Bridge.xml :
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>assemble_pos</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.basedir}/../POSMClient-Common/src/main/lib/Packaged-Web-Components</directory>
<outputDirectory>Packaged-Web-Components</outputDirectory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>www.zip</exclude>
<exclude>*.uncompressed.js</exclude>
<exclude>js/lib/cometd/*</exclude>
<exclude>.gitmodules</exclude>
<exclude>README.md</exclude>
<exclude>package.sh</exclude>
<exclude>updateDojo.sh</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.basedir}/../POSMClient-Common/src/main/native</directory>
<outputDirectory>Packaged-Web-Components/</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
</fileSets>
Note:
When i do a clean there are no exceptions coming up and is successfull, but its failing to generate the assembly jar file.
gradle clean
Its failing to generate the assembly plugin section in the build.gradle file. Kindly let me know if there is something i'm missing.
There is no like-like way of working with maven and gradle. Just running gradle init will not help to resolve most of the pom structure.
Gradle has similar plugin for distribution.
Below link has more details, on how u can write blocks of codes in build.gradle file to achieve assembly and distribution.
https://docs.gradle.org/current/userguide/distribution_plugin.html
Is it possible to tell Maven, or one of its common plug-ins, to pack one of my dependency JARs within the final assembly as a JAR file?
ie If I depend on org.some-group:some-artifact:1.2.3, the Maven plug-in would just stuff the entire some-artifact-1.2.3.jar into my final JAR file?
If I understood your question clearly, you need to add one specific jar into your generated jar, in this case you might use use classifier with maven-assembly-plugin
POM.xml
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>jar-with-dependencies-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>final-assembly</id>
<formats>
<format>jar</format>
</formats>
<dependencySets>
<!-- Include the jar-with-dependencies -->
<dependencySet>
<includes>
<include>org.some-group:some-artifact:1.2.3</include>
</includes>
<useProjectArtifact>false</useProjectArtifact>
<!-- Don't use transitive dependencies since they are already included in the jar -->
<useTransitiveDependencies>false</useTransitiveDependencies>
</dependencySet>t>
</dependencySets>
</assembly>
Above configurations might give you an idea where to start and how you can include specific jars to your final jar
When using the maven assembly plugin to build an uberjar and then package it into a zip file, I encounter a runtime failure:
java.lang.RuntimeException: could not find writer for content-type text/xml type: java.lang.String
This failure does not occur when I run my project within eclipse, or when I create and executable .jar using the eclipse Export -> Runnable Jar File so I suspect there is something wrong with the way I'm using maven creating the uberjar.
How do I fix this problem?
Turns out, the root of my problem was a conflict with the javax.ws.rs.ext.Providers file that occurs when the maven assembly plugin creates the jar. (This file can be found in the uberjar within META-INF -> services -> javax.ws.rs.ext.Providers)
The Providers file contains a list of available provider classes. Within the dependencies of my project this file exists in more than one place, and the different copies contain different provider lists. The maven assembly plugin simply chooses one version to include in the jar, and so at runtime the required "writer" class cannot be found: This class is not listed in the Providers file within the jar.
I used the maven shade plugin to overcome this problem. The shade plugin contains a facility to selectively merge duplicate files contained within the dependency tree. Within pom.xml:
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
Tells maven to merge by appending any duplicates of javax.ws.rs.ext.Providers.
Also, by setting the maven shade plugin to execute during the package phase of my build, and then the maven assembly plugin to execute at the install phase, I was able to create an executable uberjar, then package that uberjar within a .zip file, all with a simple mvn clean install invocation.
Here's what my pom.xml looks like:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
...
</parent>
<groupId>com.foo.bar</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>2.1.0.0-SNAPSHOT</version>
<name>My App</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<issues-product>MyApp</issues-product>
<issues-component>MY-APP</issues-component>
</properties>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<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>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.foo.bar.MyMainClass</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/assembly/my-app-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</project>
And here is my-app-assembly.xml:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.2 http://maven.apache.org/xsd/assembly-2.2.2.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory/>
<includes>
<include>Readme.pdf</include>
<include>config\</include>
<include>input\</include>
<include>output\</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>bin\java\</outputDirectory>
<includes>
<include>my-app.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
I have some third party jars that I want to upload to my nexus maven repo, and so far I have found two ways to do it.
Use the Nexus GUI
Use the instructions at http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
mvn deploy:deploy-file -DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<type-of-packaging> \
-Dfile=<path-to-file> \
-DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
-Durl=<url-of-the-repository-to-deploy>
I am expecting the 3rd party libraries to updated frequently say about once a quarter possibly more whenver there are security updates / releases.
Both of the two approaches above are manual you have to type a longish command or click around to make it happen. I would prefer a simple solution that is not product specific or requires lengthy command line options.
Is it possible to write a maven pom.xml that publishes a 3rd party .tar.gz just by doing mvn deploy
I am using maven 3.0.5
UPDATE Sample pom.xml that worked for me based on radai answer below.
we have a similar need here with keeping tabs of non-maven artifacts and updating them in our nexus.
the way we did it is to have a "thirdparty" project in version control which stores the non-maven artifacts, each with its own pom file.
when you upgrade any 3rd party you overwrite the old one in the thirdparty project, bunp the version in the associated pom file and run "mvn deploy" to put it into our repository.
a pom file for a "build" that results in a *.tar.gz will have a pom like this:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>myArtifact</artifactId>
<packaging>pom</packaging> <!-- so it wont auto-create any *.jars or anything -->
<build>
<resources>
<resource>
<!-- put a configuration here to copy your artifact to /target -->
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy static resource files</id>
<goals>
<goal>resources</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<!-- this creates your *.tar.gz -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>Create final ZIP package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>platform-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
with an seembly descriptor alongside it:
this one packages a bunch of stuff as *.tar.gz, retaining executable flags for *.sh files. you will need to edit to fit your needs
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>${envClassifier}</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>yourTargetDir</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>**/*.sh</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>yourTargetDir</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.sh</include>
</includes>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
</assembly>
EDIT:
if you dont need to extract/mess-with/ the *.tar.gz artifact that you have a much simpler option - use the build helper maven plugin's attach artifact goal to simply attach your *.tar.gz to be included in deployments/installs
you obviously then need to update all of the paces that depend on this new artifact version.
Here is the pom.xml I used to solve my problem based on radai suggestion to use the build helper plugin
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.thirdparty</groupId>
<artifactId>server-jre</artifactId>
<version>7.25</version>
<packaging>pom</packaging>
<distributionManagement>
<repository>
<id>thirdparty</id>
<url>http://localhost:8082/nexus/content/repositories/thirdparty/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>files/server-jre-7u25-linux-x64.tar.gz</file>
<type>tar.gz</type>
<classifier>linux-x64</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>