简体   繁体   English

如何将tools.jar包含到uberjar中(使用maven-shade-plugin)?

[英]How to include tools.jar into uberjar (using maven-shade-plugin)?

pom.xml: pom.xml中:

<?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>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.7</version>
            <scope>system</scope>
            <systemPath>${java.home}/../lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <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>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>test.Test</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

mvn install completes successfully, (ie maven sees tools.jar dependency during the build), but on execution I have mvn install成功完成,(即maven在构建期间看到tools.jar依赖),但是在执行时我有

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/api/JavacScope
    at test.Test.main(Test.java:24)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacScope
...

maven-shade plugin assumes System dependencies be present and it doesn't actually include them inside fat-jar maven-shade插件假设存在系统依赖性,并且它实际上并不包含在fat-jar中

fast dirty workaround is to install tools.jar into your maven repository and refer it as a compile scoped dependency 快速脏的解决方法是将tools.jar安装到您的maven存储库中,并将其作为编译范围的依赖项引用

better workaround is to use maven-assembly-plugin and include tools.jar as a <file> 更好的解决方法是使用maven-assembly-plugin并将tools.jar包含为<file>

Most <scope>system</scope> dependencies are not made available to plugins. 大多数<scope>system</scope>依赖项不适用于插件。

Also, including the tools.jar in your uberjar isn't terribly portable across newer versions of JVMs. 此外,在你的uberjar中包含tools.jar在新版本的JVM中并不是非常容易移植。 Its fine if you have a super controlled and limited deployment environment, but generally its better to discover the tools.jar and use it. 如果你有一个超级受控和有限的部署环境,它很好,但通常更好地发现tools.jar并使用它。

Here's an example from the maven-javadoc-plugin itself (it looks up the tools.jar and then validates that the active classloader has classes from the tools.jar) 这是maven-javadoc-plugin本身的一个例子 (它查找tools.jar,然后验证活动类加载器是否有来自tools.jar的类)

In your case, instead of looking for com.sun.tools.doclets.Taglet , you'd look for com.sun.tools.javac.api.JavacScope to verify a valid environment. 在您的情况下,您不是寻找com.sun.tools.doclets.Taglet ,而是寻找com.sun.tools.javac.api.JavacScope来验证有效的环境。

From there it would be trivial to build up a new ClassLoader with your uberjar + tools.jar and then execute what you need from a valid Thread.currentThread().setContextClassLoader() scope 从那里用你的uberjar + tools.jar构建一个新的ClassLoader,然后从一个有效的Thread.currentThread().setContextClassLoader()执行你需要的东西,这将是微不足道的.setContextClassLoader Thread.currentThread().setContextClassLoader()范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在maven-shade-plugin创建的Jar中包含测试类? - How to include test classes in Jar created by maven-shade-plugin? 如何获取 maven 阴影插件构建的 uberjar 中包含的 jar 列表? - How to get a list of the jar included in the uberjar build by maven shade plugin? 使用 maven-shade-plugin 时如何将 DefaultImplementationEntries 添加到 MANIFEST? - How to add DefaultImplementationEntries to MANIFEST when using maven-shade-plugin? 我无法使用maven-shade-plugin将maven-plugin-api打包到uber jar中 - I cannot package maven-plugin-api into uber jar using maven-shade-plugin maven-shade-plugin:如何将所有依赖项和自定义本地jar添加到可执行jar? - maven-shade-plugin : how add all dependencies and custom local jar to executable jar? maven-shade-plugin 不包括声明为依赖项的 jar - maven-shade-plugin is excluding jar that is declared as a dependency maven-shade-plugin:在战争中制作一个罐子吗? - maven-shade-plugin : make a jar whereas we are in war Project? 使用 maven-shade-plugin 加载 jar 文件 - Loading a jar-file with maven-shade-plugin 两个应用程序中的jar冲突-Maven-shade-plugin重定位 - jar conflict in two applications - maven-shade-plugin relocation maven-shade-plugin不会取代原来的jar - maven-shade-plugin doesn't replace the original jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM