我正在尝试构建简单的JAR以部署到Wildfly,但每当我尝试部署时,都会抛出很多ClassNotFoundExceptions。 我只有依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
我用于JAR的构建是:
<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>
</execution>
</executions>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</plugin>
另一方面,当我用WAR构建与WAR相同的项目时:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
</archive>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>target/classes/META-INF</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>jboss-web.xml</include>
<include>jboss-deployment-structure.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
一切正常。
如果我没有弄错,这个问题是由于类加载而产生的。
是什么导致了类加载的这种差异,我该怎么做才能避免它?
答案 0 :(得分:0)
您应该部署由shade插件生成的jar。 完成干净安装后,您将获得两个罐子。一个是常规jar,只包含源代码的编译类,另一个jar(将由shade插件创建)将包含已编译的类以及所有依赖项中的类。这是您应该部署到服务器的jar。由于此jar将包含所有依赖项jar中的类,因此不会获得任何ClassNotFoundExceptions。