How to generate resource files from parent POM in Maven? - java

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.

Related

Maven dependency plugin adds artifact named directory

I want to copy files from some artifact. but it always adds a directory with the name of that artifact.
the pom of the artifact to copy from:
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Scheduler common test resources</name>
<description>A scheduler test resources</description>
<packaging>pom</packaging>
.
.
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>lib/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
the assembly file:
<assembly>
<id>json</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>resources/db</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>alterTables.sql</include>
<include>createTables.sql</include>
<include>insertsIntoReminders.sql</include>
</includes>
<excludes>
<exclude>pom.xml</exclude>
</excludes>
</fileSet>
</fileSets>
the item to be copied in the artifact pom:
<artifactItem>
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<outputDirectory>${project.build.directory}/test-classes/db/</outputDirectory>
<type>tar.gz</type>
<overWrite>false</overWrite>
</artifactItem>
result:
its get copied to test-classes/db/scheduler-common-test-resources-1.0.0-SNAPSHOT/
how can i remove the directory with the artifact name?
The assembly-plugin will by default add a baseDirectory, which will, also by default, be ${project.build.finalName}.
In your case, you just have to indicate the plugin that you don't need that directory by adding:
<includeBaseDirectory>false</includeBaseDirectory>
in the assembly descriptor (assembly.xmlfor you). See assembly descriptor documentation.

maven pom to gradle - assembly plugin failure

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

Include entire dependency JAR as JAR within maven project

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

Maven maven-assembly-plugin jar-with-dependencies exclude LICENSE.txt from final jar

I am using Maven to pack a single executable jar and I can't seem to figure out how to make it not put the LICENSE.txt and NOTICE.txt files inside it. These files are found inside the final executable .jar, but I have no such files in my project directory.
I tried multiple configurations that didn't work. The one I am at now is the following:
Inside my pom.xml I have:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
<excludes>
<exclude>README*</exclude>
<exclude>LICENSE*</exclude>
<exclude>NOTICE*</exclude>
</excludes>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>maven-assembly-plugin</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I also tried using inside the pom.xml
<descriptors>
<descriptor>/src/assembly/src.xml</descriptor>
</descriptors>
instead of
<excludes>
<exclude>README*</exclude>
<exclude>LICENSE*</exclude>
<exclude>NOTICE*</exclude>
</excludes>
with the content of /src/assembly/src.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>distribution</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<excludes>
<exclude>README*</exclude>
<exclude>LICENSE*</exclude>
<exclude>NOTICE*</exclude>
</excludes>
<!--useDefaultExcludes>false</useDefaultExcludes-->
</fileSet>
</fileSets>
</assembly>
but for this I am getting Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (maven-assembly-plugin) on project DatabaseTaseng: Failed to create assembly: Error creating assembly archive distribution: A zip file cannot include itself -> [Help 1]
How should I configure it to get a single executable jar with dependencies without those .txt files?
The exclusions tag is to exclude jars dependencies.
The dependencySet tag allows for an unpackOptions which has the options to include/exclude files when the dependencies are being unpacked.
So your assembly should look something like:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<unpackOptions>
<excludes>
<exclude>**/README.txt</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>
I don't use filesets...
I use this configuration, taken from here:
http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html
<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>distribution</id>
<formats>
<format>jar</format>
</formats>
<files>
<file>
<source>README.txt</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>LICENSE.txt</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>NOTICE.txt</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
This configuration should filter file you need at the moment of the generation of the ouput jar...so, it should filter also files that will be generated...
I hope this works for you...

POM that zips the project

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>

Categories

Resources