Maven shade plugin how to add package level resources - maven

I am using shade plugin to build a fat or uber jar . Please see my shade plugin pom snippet as below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
I have some resource file for XML and properties which are not in src/main/resources but lies along with java packages.
When I build the code with maven with mvn clean package it compiles and add all dependent jar file but all resources files that are at the package level say src/main/java/x/y/z/hibernate.hbm.xml doesnt get added into respective place of fat jar.
Could someone please let me know what extra I have to do in above maven snippet to add those resources
I tried adding . in the artifacts set it didn't work . Also using maven assembly plugin its not working. I also searched direct eclipse plugin which can help but no clue. please help

Related

Package com_atlassian_clover is not visible

I am experimenting with java modules and unit tests.
So I also have the clover plugin within my maven pom.xml:
<plugin>
<groupId>org.openclover</groupId>
<artifactId>clover-maven-plugin</artifactId>
<version>4.2.0</version>
<configuration>
<excludes>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
But when running
mvn clover:instrument
I got the following error:
error: package com_atlassian_clover is not visible
(package com_atlassian_clover is declared in the unnamed module, but module com_atlassian_clover does not read it)
How could I fix that? Or do I have to wait until clover will support java modules?
I think it would be not a good idea to include the clover package within my modules-info.java. (Might be a solution, because clover instruments the code).

Download source via maven with maven-dependency-plugin

I want to download a specific sources jar (guice-sources.jar) and I don't want it to download android sources. This would work on the terminal:
mvn dependency:sources -DincludeArtifactIds=android
and this works well too:
mvn dependency:sources -DincludeArtifactIds=guice
I config a plugin segment in the pom.xml as below,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>sources</goal>
</goals>
<configuration>
<includeArtifactIds>guice</includeArtifactIds>
<!--<excludeArtifactIds>android</excludeArtifactIds>-->
</configuration>
</execution>
</executions>
</plugin>
and run this,it still downloads the android jar for me
mvn dependency:sources
I have tried to move the plugin part in a pluginManagement, didn't work either.
What's wrong with it?

Maven pluginManagement configuration inheritance strange behavior

I'm using pluginManagement element in my parent pom.xml to configure plugins for all its children. For example, I have the following configuration:
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-artifacts</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>some/where/else</outputDirectory>
<resources>
<resource>
<directory>some/another/resource</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>deps/dir</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</pluginManagement>
The official documentation states that a plugin configured in pluginManagement still has to be added to plugins element in children poms. Indeed, if I remove this from child pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
then maven-dependency-plugin stops firing at install phase. However, it seems that it does not affect some other plugins, namely, maven-resource-plugin. Even if I do not have
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
in my child pom, its copy-resources goal still fires at install phase and performs the work it is configured to do.
Why is this behavior present? Is there a list of plugins which are inherited always, or maybe I'm missing something?
The whole POM isn't visible; but given the behavior you're describing this is a jar, war, or ear, correct? The resource plugin is defined for those packaging types by default. It includes an execution that copies resources (as described by #maba).
Since the plugin definition is included in your child POM (even though YOU didn't put it there directly), Maven merges the execution defined in the <pluginManagement> section with the execution provided by Maven.
There is documentation describing the default lifecycle bindings by packaging type. Note the dependency plugin isn't mentioned; but resources is; that's why you observe the difference. Running with -X will show the plugin executions.
Maven always copies resources that are inside src/main/resources by default.
From Maven Getting Started Guide:
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.

maven-site-plugin not generating apidocs folder with javadocs

I inherited a project that is supposed to build javadoc files and place them in the site directory. This is not being done. I have looked at all the examples I can find and I can't figure out where the configuration is broken.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</reportPlugins>
</configuration>
</execution>
</executions>
</plugin>
Any and all help is greatly appreciated.
You have bound maven site plugin's site goal to prepare-package phase. You have configured javadoc generation in this plugin configuration.
As such, if you run maven's default lifecycle goals like mvn package or mvn install you should get site report with javadocs.
If you ran mvn site, it would skip prepare-package phase to which your plugin configuration is bound and hence would not generate javadoc.

Recommended practices for distributing/using maven projects outside the build lifecycle

There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.
Things I have tried
1) Maven-shade plugin
For some use cases ( such as hadoop jobs ) I find it convenient to use the Maven shade plugin to integrate with the package step ( it builds an all inclusive uber-jar ). The downside is that the uber-jar is too massive. Also, I cant get the maven shade to work on datanucleus dependencies as it messes up something.
2) distribute the dependencies along with the jar.
//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass
generates a directory with all the dependencies along with the jar.
What I would like to do is
3) just be able to distribute the jar and handle the dependencies while executing the jar. Since mvn package puts the pom in the jars manifest folder, all the information is there right ? . Now, I would like it if there was a one line command to be able to run this jar asking maven to manage the dependencies. Even more awesome if someone knows if such a thing can be used as a hadoop job.
I think there is no solution for question in your point 3.
My three options, in all cases you can run application with simple java -jar target/project.jar. Nothing more is needed.
1. Maven Assembly Plugin with jar-with-dependencies descriptor
(manifest in jar configuration is missed here)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
2. Maven dependency plugin
Maven dependency plugin, properly jar configured (classpath) and maven assembly custom descriptor for packing all together.
(you can simplify this, if your project is not using snapshot dependencies)
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.package.MainApp</mainClass>
<packageName>your.package</packageName>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}.$${artifact.extension}/customClasspathLayout>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeScope>test</excludeScope>
<includeScope>compile</includeScope>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
3. Maven One Jar
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Resources