-->

I wish to exclude some class files from my jar. I

2019-03-26 16:13发布

问题:

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>

回答1:

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>


回答2:

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

  1. Project beans-conversion has files application.properties, logback.xml, and logback.xsd
  2. Project extract-conversion has also files called application.properties, logback.xml, and logback.xsd
  3. 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>