I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error - maven

I dont get any error with this code. Just that the file that I want to exclude still gets added. I am using the maven plugin for eclipse
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>only</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<excludes>
<exclude>**/com/uiservices/controllers/*.* </exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

The maven-assembly-plugin doesn't work like that.
There what you want to do is override the configuration of the assembly descriptor jar-with-dependencies and that's not possible.
If what you want to do is create a jar similar to the one created by the assembly jar-with-dependencies but without some specific classes of your own project, you have to write your own assembly and call it in the maven-assembly-plugin like what follows.
Assembly in src/assembly/jar-with-deps-with-exclude.xml :
<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-and-exclude-classes</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>${project.build.outputDirectory}</directory>
<excludes>
<exclude>com/uiservices/controllers/*.*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
This will create an assembly with no the dependencies unpacked and with your classes added except the ones excluded.
And then in your pom.xml :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>only</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/jar-with-deps-with-exclude.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But if what you need is your classical jar without excluded classes, you can exclude them in the maven-jar-plugin directly :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/uiservices/controllers/*.*</exclude>
</excludes>
</configuration>
</plugin>

I had a similar issue with maven-assembly-plugin:
Project beans-conversion has files application.properties,
logback.xml, and logback.xsd
Project extract-conversion has also files called application.properties, logback.xml, and logback.xsd
Requirement is extract-conversion.jar should include beans-conversion.jar content but application.properties, logback.xml, and logback.xsd of extract-conversion.jar should override the beans-conversion.jar.
Solution:
We solved this using maven-shade-plugin as below in extract-conversion's pom.xml.
...
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>beans-conversion</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
...
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>logback.xml</include>
<include>logback.xsd</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.mycompany:beans-conversion</artifact>
<excludes>
<exclude>application.properties</exclude>
<exclude>logback.xml</exclude>
<exclude>logback.xsd</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

using maven-shade-plugin
ref: Maven dependency: exclude one class
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-jar-with-dependencies</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.taobao.arthas.core.Arthas</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:termd-core</artifact>
<excludes>
<exclude>META-INF/services/io.termd.core.spi.ConfigProvider</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

Related

Spring-Boot External lib folder with Maven

I need externalize my application's dependencies. I want to achieve the following layout for my Spring Boot Application
.
├── *.properties
├── static
├── main.jar
└── lib
└── *.jar
Where lib folder contains my dependencies.
I try with the maven-jar-plugin and spring-boot-maven-plugin but I get the following error:
Could not find or load main class com.Application
My pom.xml
<profile>
<id>dist</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>alfred</finalName>
<excludes>
<exclude>src/main/resources/*</exclude>
<exclude>*.properties</exclude>
<exclude>*.html</exclude>
<exclude>**/assembly/*</exclude>
<exclude>**/lib/*</exclude>
<exclude>**/static/*</exclude>
<exclude>**/templates/*</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<Main-Class>com.Application</Main-Class>
</manifest>
<manifestEntries>
<Class-Path>./alfred_lib/**</Class-Path>
<Class-Path>./**</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-1</version>
<configuration>
<descriptor>src/main/resources/assembly/dist.xml</descriptor>
<finalName>alfred</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
My src/main/resources/assembly/dist.xml:
<assembly>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/assembly</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**.sh</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<!-- Estáticos -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory></outputDirectory>
<excludes>
<exclude>lib/**</exclude>
<exclude>assembly/**</exclude>
</excludes>
</fileSet>
<!-- Aplicación -->
<fileSet>
<directory>target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<!-- Dependencias -->
<dependencySets>
<dependencySet>
<outputDirectory>alfred_lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
I've been reading for several days and I have discarded:
Using spring-boot-thin-layout
Create far jar: It's a project requirement.

The following patterns were never triggered in this artifact inclusion filter: o 'com.alibaba:dubbo

Following are the contents for the assembly.xml
<assembly>
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>META-INF/**</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</unpackOptions>
<includes>
<include>com.alibaba:dubbo</include>
<include>commons-codec:commons-codec</include>
<include>org.apache.httpcomponents:httpclient</include>
<include>org.apache.httpcomponents:httpcore</include>
<include>org.apache.commons:commons-csv</include>
</includes>
</dependencySet>
</dependencySets>
I have added following dependency to the pom.xml -
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.9</version>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>prod-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>jar-for-testcase</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly_test.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>pkg</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>pkg_assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run maven install, I am getting the warning and my code doesn't work. But if I run the code directly without creating the jar then it works just fine. So there is something wrong with the jar that get created. Debugging this, I came across this warning. But I am unable to understand what wrong am I doing. According to my understanding the include tag should contain group-id:artifact-id. I am doing this but still I am getting the warning. Can someone put some light on what am I missing here.
Resolved the warning with the following replacement
<include>com.alibaba:dubbo:jar:</include>
Just for reference, following is the long pattern groupId:artifactId:type:classifier

How to include particular file from a folder in maven install target directory?

Directory structure is
conf
axis2.aar
com
lib
META-INF
axis2.xml
build.xml
common-logging.properties
log4j.properties
*.class
I want that only axis2.xml should be included in axis2.aar in target folder when doining maven install. I have tried many include and exclude combinations but nothing is
working. After doing maven install, It is taking whole axis2.aar contents. I also tried filtering by setting it to true.Please suggest some solution.
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>
<groupId>CUDB_HSS</groupId>
<artifactId>CUDB_HSS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>wsdl</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>conf/axis2.aar</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.6.4</version>
<extensions>true</extensions>
<executions>
<execution>
<id>cudb-hss-aar</id>
<phase>package</phase>
<goals>
<goal>aar</goal>
</goals>
<configuration>
<aarDirectory>conf/axis2.aar</aarDirectory>
<aarName>axis2</aarName>
<filesets>
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss</id>
<phase>package</phase>
<configuration>
<classifier>client1</classifier>
<finalName>cudb-hss</finalName>
<includes>
<include>com/accenture/**/*.class</include>
<include>macro_CUDB_Accenture_HSS.class</include>
<include>**/com/accenture/il/interfaces/cudb/hss/conf/jaxb/**/*.properties</include>
<include>**/com/accenture/il/interfaces/cudb/hss/error/bean/jaxb/**/*.properties</include>
<include>**/*.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss-wsdl</id>
<phase>package</phase>
<configuration>
<classifier>client2</classifier>
<finalName>cudb-hss-wsdl</finalName>
<includes>
<include>com/ericsson/**/*.class</include>
<include>**/com/ericsson/**/jaxb.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The answer to your question can be:
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>axis2.xml</include>
</includes>
</fileset>

Generate several war for spring boot application

It seems that spring boot will repackage the package generated by maven package phase, and then repacke the war to make it executable.
Now I want to genrate multiple wars for different environments by a single maven command, I tried to use maven-assembly-plugin:
1 unzip the war generated by `spring-boot-maven` plugin to a directory
2 Assembly with the files in the directory, and add some other filtered resources
3 create the war
Check this post:generate multiple artifacts in maven
While it works, and I got multiple wars, but none of them can be executeable by java -jar xx.war. It seems that the classes are corrupted.
So I wonder if there is an alternative solution?
update my pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/as-common</source>
<source>src/main/as-server</source>
<source>src/main/as-app</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- unzip the contents of the war(executeable) generated by spring-boot to a certain directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>extract_spring_war</id>
<phase>package</phase>
<configuration>
<target>
<echo message="extract war generated by spring-boot-maven-plugin"/>
<delete dir="${basedir}/target/${project.build.finalName}-spring" includeemptydirs="true"/>
<unzip src="${basedir}/target/${project.build.finalName}.war" dest="${basedir}/target/${project.build.finalName}-spring/"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>test</item>
<item>dep1</item>
<item>dep2</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
</plugin>
<goal>single</goal>
<configuration>
<archive>
<manifest>
<mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
assmbly.xml:
<assembly>
<id>${item}</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!-- file from the unpacked contents -->
<fileSet>
<directory>target/${project.build.finalName}-spring</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
<filtered>true</filtered>
</fileSet>
<!-- add environment awared resources -->
<fileSet>
<outputDirectory>/WEB-INF/classes</outputDirectory>
<directory>${basedir}/src/main/custom/</directory>
<includes>
<include>${item}.properties</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Update:
At first I got the error:
No main class detected
Then I add the following for maven-assembly-plugin,
<archive>
<manifest>
<mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
</manifest>
</archive>
After that I repackage the wars, and I got error when I ran:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 4022320623 in class file org/springframework/boot/loader/WarLauncher
The class files are being corrupted when you're repackaging the war file. I suspect that's because you're filtering all of the files:
<fileSet>
<directory>target/${project.build.finalName}-spring</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
<filtered>true</filtered>
</fileSet>
You don't want to filter binary files as they may happen to contain data that looks like a ${} placeholder. You should update the assembly to avoid applying the filtering to class files. The simplest way to do that would be to disable filtering entirely:
<fileSet>
<directory>target/${project.build.finalName}-spring</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
<filtered>false</filtered>
</fileSet>
If you need filtering for text files, you should use two fileSets. One that includes the binary files and disables filtering, and one that includes the text files and enables filtering.

Maven Assembly Create Custom Format

As part of a bigger project, I want to package some items I download together with some jar files as a .tar.gz package. And I did that successfully.
But now I want to "rename" that .tar.gz to something custom (e.a. to mypackage.banana)
Is there an easy way to achieve this?
What I have so far id accomplished using https://github.com/maven-download-plugin/maven-download-plugin
<build>
<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>${some.jar.file.url}</url>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<url>${some.models.zip.url}</url>
<unpack>true</unpack>
<outputDirectory>${project.build.directory}/data</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version> <!-- Old -->
<executions>
<execution> <!-- Build tar.gz archive. -->
<id>tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/tar.xml</descriptor>
</descriptors>
<finalName>project-${project.version}-el6</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution> <!-- /NewStuff -->
</executions>
</plugin>
</plugins>
</build>
using tar.xml:
<assembly>
<id>tar</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>target/data</directory>
<outputDirectory>/data</outputDirectory>
</fileSet>
<fileSet>
<directory>target/lib</directory>
<outputDirectory>/lib</outputDirectory>
</fileSet>
<fileSet>
<directory>meta</directory>
<outputDirectory>/meta</outputDirectory>
</fileSet>
</fileSets>

Resources