I want to exclude some class files from my jar. I am using maven-assembly-plugin. It is still adding files. I get no error

I am not mistaken with this code. Just the file that I want to exclude is still being added. I am using 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> 
+9
source share
3 answers

maven-assembly-plugin does not work.

What you want to do is override the configuration of the assembly descriptor jar-with-dependencies and this is not possible.

If you need to create a jar similar to the one created by the jar-with-dependencies assembly, but without specific classes of your own project, you need to write your own assembly and call it in maven-assembly-plugin as it should.

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 the assembly without unpacking the dependencies and add your classes except those that are 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 you need your classic jar without excluded classes, you can directly exclude them in maven-jar-plugin :

 <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>com/uiservices/controllers/*.*</exclude> </excludes> </configuration> </plugin> 
+13
source

I had a similar problem with maven-assembly-plugin :

  • The beans-conversion project has application.properties , logback.xml and logback.xsd
  • The extract-conversion project also has files called application.properties , logback.xml and logback.xsd
  • The extract-conversion.jar requirement should include beans-conversion.jar content, but application.properties , logback.xml and logback.xsd of extract-conversion.jar should override beans-conversion.jar .

Decision:

We solved this using the maven-shade-plugin , as shown below in extract-conversion 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> 
0
source

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> 
0
source

Source: https://habr.com/ru/post/986476/


All Articles