I would like to document in the shaded jar what maven artifacts actually end up in that shaded jar.
All the packages get merged and that makes it difficult to workout exactly what artifacts went into it just by looking at the jar.
I suppose the ideal place for that information would be the manifest file but it could just be in a text file.
Ideally I want to see groupId, artifactId and version.
Is this at all possible with the maven shade plugin?
Thanks in advance, Phil.
You can do this with maven, below steps to follow:
1- Create under src/main/resources a file wich will contain the information, information.txt for example with the following content:
version=${project.version}
artifactId=${project.artifactId}
groupId=${project.groupId}
2- Activate Maven filtring
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/information.txt</include>
</includes>
</resource>
...
</resources>
...
</build>
...
</project>
3- Build your project. The file will contain now the information you need.
More information about the plugin.
As it doesn't like this is supported by the shade plugin i have requested this feature.
https://issues.apache.org/jira/browse/MSHADE-236
When building jars with Maven, by default you will get the following entries: /META-INF/maven/${groupId}/${artifactId}/pom.properties and /META-INF/maven/${groupId}/${artifactId}/pom.xml. When shading, all these files will also end up in the shaded jar.
Related
I'm trying to create a Spring project structure where I have my HTML templates stored in the same src/main/java package (in Tapestry-like way), so the structure basically looks like this:
src/main/java
-org
-example
-views
-pageOne
PageOneController.java
Template.html
-pageTwo
PageTwoController.java
Template.html
However after packaging a WAR while using Maven I get a structure like this:
WEB-INF/classes
-org
-example
-views
-pageOne
PageOneController.class
-pageTwo
PageTwoController.class
So the .html files are ignored and obviously nothing works.
I tried to configure maven-resources-plugin to copy those resources like this:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>*/**.html</include>
</includes>
<excludes>
<exclude>*/**.java</exclude>
</excludes>
</resource>
</resources>
</build>
but to no avail.
How do I configure my project to copy non-java files from src/main/java into the final output?
It turns out that the problem was with maven modules. I have declared resources plugin in the main pom.xml instead of the module's pom.xml which lead to the configuration issues.
I have a maven plugin that exposes a Mojo, with a goal that runs at the compile stage. The project was generated using mvn archetype:generate, and the POM contains all the standard stuff that comes with running that, very little deviation. The project includes a couple of resource files, e.g. filea.txt and fileb.txt, that are packaged up as part of the jar.
When the plugin is used in a project, I'd like the files that are included in the jar to be extracted and copied to the target\test-classes directory of the host project. I'm trying to use the plugin jar to both distribute some files + expose some functionality that can then use those files.
Is this a valid approach, and if so, are there settings I can add to the plugin POM to indicate that content from the plugin should be extracted and copied? I want to centralise this logic in the plugin, rather than having to do in the plugin host.
I feel like it's something with maven-dependency-plugin or maven-resources-plugin or build-helper-maven-plugin:attach-artifact, have tried a couple of different approaches but think I'm missing something obvious:
e.g. something like this in plugin POM?
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<outputDirectory>${basedir}/target/test-classes</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>filea.txt</include>
<include>fileb.txt</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
</plugin>
// etc etc
Google fu has let me down, keep ending up on maven resources page. Can post directory structure / more information if needed.
Cheers
First I would suggest to put resources which needs to be distributed into src/main/resources which looks like you have done ...but remove the configuration for the maven-resources-plugin and let maven do it's work. This is automatically copied into target/classes/ which in result is packaged into the resulting jar later.
If your plugin needs to get those files those can accessed as a usual resource via this.getClass().getResourcesAsStream("/...") and reading and writing them into a new location preferable into target/...
I have a multi-module maven project. I use maven build helper plugin to automatically add generated sources to the classpath.
I am able to use the generated sources of module-X in module-X, however, when I add module-X as a dependency to module-Y, the generated sources of module-X are not visible because they are not included in the X.jar file.
Is there a way to include the generated sources in the jar file or force maven to generate sources of dependencies?
You can explicitly specify that the generated classes should be part of the output jar file:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<includes>
<include>generatedClassesFolderPath</include>
</includes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Replace the generatedClassesFolderPath with the relative path of the folder where the generated classes are.
More info:
How to include/exclude content from jar artifact
I had the same question and I solve it as follows:
Add an application class under package such as src/main/java/com..... in your module-X and add a #SpringBootApplication annotation. In addition to this, the application class can be no content.
Make sure module-X in module-Y dependencies and restart `module-Y.
I have a Jenkins build server that automatically builds the project into a jar when a new commit has been pushed to the GitHub project. I looked at file size of the artifacts that Jenkins creates and I was surprised. I came to the conclusion that all the third-party dependencies were included in the jar artifact!
I don't need Maven to include them in the artifact as that will increase the size dramatically and it isn't useful. So I fiddled around with my pom.xml file, but I couldn't get it working. The dependencies keep being included in the jar.
I'm relatively new to Maven and I would appreciate it a lot if someone can help me out!
Sources (if you need any):
Pom.xml
You can avoid packaging your dependencies inside your jar file by providing the scope they should be wrapped in. But since I looked to your pom.xml descriptor and find nothing misconfigured, I will suggest to use the maven-jar-plugin to exclude all third party libraries as follows:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<excludes>
<exclude>*.jar</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Hope this helps.
BR.
My guess is that this is the root cause:
<resource>
<directory>libs</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
if you (also) have all your jars in this folder, then they will end up on the classpath ( see target/classes ), hence they will be part of the jar.
You only have to specify the jars as dependencies, Maven will do the rest.
Resources are used for non-java (or non-compilable) files, which should end up at the classpath as well, like config files for Spring or Hibernate
I have Maven multi-module project with such structure:
parent-pom-project
-- module1
-- module2
At the parent-pom-project I have such pom.xml
<modules>
<module>module1</module>
</modules>
...
<profiles>
<profile>
<id>local</id>
<properties>
<prop>local_prop</prop>
</properties>
</profile>
<profile>
<id>test</id>
<modules>
<module>module2</module>
</modules>
<properties>
<prop>test_prop</prop>
</properties>
</profile>
</profiles>
At all pom.xml files I have such tag:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
At module1 and module2 in resource directory I have properties files with such text:
prop=${prop}
The problem is that after
mvn clean install
or
mvn clean install -Ptest
or even
mvn clean install -P test
I get
prop=local_prop
If I user test profile for build module2 is also builded, but properties are used from local profile.
I use Maven 3.0.3.
Anybody have any ideas?
You could try to use the mvn help:effective-pom -Ptest command to see the paramters used in your build.
See http://maven.apache.org/plugins/maven-help-plugin/plugin-info.html for more details.
Add a ${basedir} in front of your resource directories:
<directory>${basedir}/src/main/resources</directory>
This should fix your problem. My explanation would be that in a multi-module project it's not picking up the path correctly (for within the child module), if you're building from the top-level. Thus when trying to filter, it applies it to a different directory (the actual root-level aggregator), instead of the child.
I hope it helps.
I can't figure out how maven can resolve your property if you do not specify any profile. So, to see what's really there, I tried myself, following exactly the schema you described and... I did not experience the problem you have. In your case, it really behaves like if the property was defined outside the profile -as bugske suggested. What happened if you comment temporarily both profiles ?
I resolve problem uninstalling current maven plugin for eclipse and use another one.
Now I use this ones:
- Maven Integration: http://m2eclipse.sonatype.org/sites/m2e
- Maven Integration for WTP: http://m2eclipse.sonatype.org/sites/m2e-extras/
Early I was using this one http://download.eclipse.org/technology/m2e/releases/. I cannot explain such behavior but may be some configuration was changed by plugin.
Although old I had the same problem and didn't find the solution here. For me the problem was Eclipse which I use parallel to mvn on the command line. Eclipse instantly called process-resources after I did so on the command line.
Thus the solution was to select the profile in Eclipse (Project->Maven->Select Maven Profiles).