maven-shade-plugin不包含org / apache / commons / vfs2 / impl / StandardFileSystemManager

时间:2017-07-18 15:52:25

标签: java eclipse apache maven

我有一个包含org.apache.commons的commons-vfs2库的项目,我想用maven-shade-plugin“遮蔽”它,并且还要将它最小化。

问题是,在我对jar进行着色后,我运行它并告诉我它找不到类org.apache.commons.vfs2.impl.StandardFileSystemManager。

实际上创建的jar没有那个类。如何使用shade插件删除该类?我怎么能说插件保留那个类?

这是我的pom

<?xml version="1.0" encoding="UTF-8"?>
<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>blah</groupId>
<artifactId>MyApp</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <!-- Maven Source Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Maven JavaDoc Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.4</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Maven Deploy Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
        </plugin>
        <!-- Maven Shade Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <minimizeJar>true</minimizeJar>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>blah.MyMainFrame</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>
    </plugins>
</build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <!-- Apache -->
    <org.apache.commons.lang3.version>3.6</org.apache.commons.lang3.version>
    <org.apache.commons.collections4.version>4.1</org.apache.commons.collections4.version>
    <org.apache.commons.vfs2.version>2.1</org.apache.commons.vfs2.version>
    <org.apache.commons.io.version>2.5</org.apache.commons.io.version>

    <!-- Lombok -->
    <org.projectlombok.version>1.16.18</org.projectlombok.version>

    <!-- Jackson -->
    <com.fasterxml.jackson.version>2.8.9</com.fasterxml.jackson.version>

    <!-- Logging -->
    <org.slf4j.version>1.7.25</org.slf4j.version>
    <ch.qos.logback.version>1.2.3</ch.qos.logback.version>

    <!-- JUnit -->
    <junit.version>4.12</junit.version>
</properties>

<dependencies>
    <!-- Apache -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${org.apache.commons.lang3.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${org.apache.commons.collections4.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-vfs2</artifactId>
        <version>${org.apache.commons.vfs2.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${org.apache.commons.io.version}</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${org.projectlombok.version}</version>
    </dependency>

    <!-- Jackson -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${com.fasterxml.jackson.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${ch.qos.logback.version}</version>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我在此代码中使用VFS2:

import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;

public class FileListenerManager{

    private FileSystemManager fsManager;
    private DefaultFileMonitor fm;

    public FileListenerManager(FileListener fl) throws FileSystemException{
        fsManager = VFS.getManager();

        fm = new DefaultFileMonitor(fl);

        start();
    }

    public void addFile(File file) throws FileSystemException{
        fm.addFile(fsManager.resolveFile(file, file.getAbsolutePath()));
    }

    public final void start(){
        fm.start();
    }

    public final void stop(){
        fm.stop();
    }

}

正如您所看到的,没有(显式)引用StandardFileSystemManager。

我希望插件能够处理从最终的超级jar中删除的类或方法。

从VFS.class文件中我可以看到我调用的方法VFS.getManager()是

public static synchronized FileSystemManager getManager() throws FileSystemException{
    if(instance == null){
        instance = createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
    }
    return instance;
}

我想知道为什么插件无法感知StandardFileSystemManager类的用法。

谢谢。

1 个答案:

答案 0 :(得分:0)

您已选择启用minimizeJar选项:

<minimizeJar>true</minimizeJar>

这是做什么的:

  

自动删除项目未使用的所有依赖项类,从而最大限度地减少生成的uber JAR。

但它如何确定项目使用哪些类取决于您的代码,因此我们无法重现。

如果删除该行,您的超级jar将包含所有依赖项。然后,您可以使用排除项手动选择您不想要的部分。

或者您可以保留它并手动选择被排除但您知道自己需要的部分。

有关正确使用它的详细信息,有this Q&A