Maven - export a jar with a bundled file - java

I am trying to export a jar with a bundled file (readme.md) via the pom.xml
I want the exported jar to look like this:
I tried using maven jar plugin as depicted below. However, I get a java file but no readme.md inside it.
Here is my plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>src</classesDirectory>
<includes>
<include>**/*dummy/Dummy*</include>
<include>**/*.md</include>
</includes>
<finalName>mydummyjar</finalName>
</configuration>
</execution>
</executions>
My file-system looks like this:
How I get the structure in the picture above?
Thanks in advance.
Update
Thanks for Veselin Davidov, The next code worked for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>/</classesDirectory>
<includes>
<include>**/*dummy/Dummy*</include>
<include>readme.md</include>
</includes>
<finalName>mydummyjar</finalName>
</configuration>
</execution>
</executions>

Related

How to delete a file while doing maven install (build) from target directory

I am looking to delete a folder (api-docs) from the target folder. When I do maven build, when the target folder is generated, it should exclude that folder(api-docs). Target contains Classes, codegen, generated-sources, javadoc-bundle-options, maven-archiver, maven-status, test-classes and a war file. I need to exclude(api-docs) which is present in codegen Codegen > generated-sources> web> api-docs( contains css, fonts, images, lang, lib, specs and some other js and html files)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>build</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset> dir="${project.build.outputDirectory}/target/codegen/generated-sources/web/api-docs"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
`
I added that in pom.xml, but couldn't able to delete. Please suggest
Here is the entire contents of build from pom file
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.xxx.chassis.api.plugins</groupId>
<artifactId>codegen-maven-plugin</artifactId>
<version>${codegen.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<templateDir>chassis-archetypes</templateDir>
<configFile>${project.parent.basedir}/codegen-config.yaml</configFile>
<specifications>
<specification>${project.parent.basedir}/partnerships-originations-product-offer-id.yaml</specification>
</specifications>
<basePackage>com.xxx.papi.popoi</basePackage>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>build</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs/swagger-ui.min.js"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${codegen.generated-sources}/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<sourcepath>${codegen.generated-sources}/java</sourcepath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Please share the entire contents of the build->plugins section of your pom file. It should not be necessary to add the maven-antrun-plugin to your pom. The apidocs folder in your target directory is usually produced by the maven-javadoc-plugin which you possibly have present in your pom file. The first way to prevent the creation of the apidocs folder would be to remove that plugin declaration from your pom. An alternative way would be to supply the maven.javadoc.skip argument as per this answer: https://stackoverflow.com/a/9360317/7810853
The goal for the antrun-plugin is run, not build. Changing the pom.xml accordingly should help:
[...]
<goals>
<goal>run</goal>
</goals>
[...]
You also should double check in which phase the api-doc folder is created and you should call the ant-plugin in a later phase, for example package:
[...]
<phase>package</phase>
[...]
As a third point, the Maven variable ${project.build.outputDirectory} usually references the folder target/classes. Please check this answer for more details. Therfore, you could either use
<fileset dir="${project.basedir}/target/codegen/generated-sources/web/api-docs"/>
or - more precise -
<fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs"/>

How to get PMD maven plugin to skip generated source code?

So I'm creating a maven plugin using the maven-plugin-plugin. The HelpMojo in maven-plugin-plugin generates a java source file.
Unfortunately, PMD is picking this up and complaining about it. Is there a way to have PMD ignore just a single source file? Thanks!
Maven PMD Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<id>pmd-verify</id>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
<configuration>
<printFailingErrors>true</printFailingErrors>
</configuration>
</execution>
</executions>
</plugin>
Generated sources usually end up (with maven) in a subdirectory in target/generated-sources, for the maven-plugin-plugin it's target/generated-sources/plugin.
You can exclude these complete directories with excludeRoots, e.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<id>pmd-verify</id>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
<configuration>
<printFailingErrors>true</printFailingErrors>
<excludeRoots>
<excludeRoot>target/generated-sources/plugin</excludeRoot>
</excludeRoots>
</configuration>
</execution>
</executions>
</plugin>
There is also a file based exclude option.

Sign all jars and wars maven project

How to sign all jars and wars that are generated in project when i clean and build with maven ? Is there such plugin and instruction how to use it. I found this http://maven.apache.org/plugins/maven-jarsigner-plugin/usage.html while searching the internet but there is no good instruction how to use or test it.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>sign</id>
<phase>install</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<archiveDirectory>jars location</archiveDirectory>
<includes>
<include>**/*.jar</include>
</includes>
<keystore>/path/to/keystore.jks</keystore>
<alias>...</alias>
<storepass>...</storepass>
<keypass>...</keypass>
</configuration>
</plugin>
This code worked for me

How to package a jar and all dependencies within a new jar with maven

I have a maven project with like ten dependencies. Before, I used to pack all of that in a single jar thanks to maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>myApp.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
But now, I added a step before. I have a plugin that will generate the jar of my application. So I just want the assembly plugin to add the dependencies to this jar. Unfortunately, the plugin doesn't use this jar, but instead, seems tu be using the result from the compiler.
is there a way to specify that I want the plugin to use the previously generated jar instead of the result from the compiler ?
Try using the maven-shade-plugin. You need something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

Exclude packages while generating sources jar in Maven

I would like to generate a sources jar but without some packages in my project.
Now I have this in my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
How to exclude specific packages?
similar but with netbeans environment
You can configure the maven-source-plugin with the excludes attribute:
List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents is being packaged into the JAR.
A sample configuration where you would exclude every class under the package com.my.package would be the following:
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
<phase>package</phase>
<configuration>
<excludes>
<exclude>com/my/package/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

Categories