[英]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.