工作中遇到一个问题,
使用openjdk1.8 + maven-war-plugin:2.5 进行打包时会报错。使用oraclejdk 1.8则不会报错
Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
</plugin>
</pligins>
当你给插件配置了时就会报上面提到的错误
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
解决方案有2种
方案一,直接升级插件2.5到3.2.2
方案二,解决抛异常的真正原因,是由于maven-war-plugin:2.5 使用了反序列化 xstream:1.4.4抛出的异常
根据社区的信息
https://github.com/x-stream/xstream/issues/360
大致意思是:
11 年前(也就是2013年) XStream 1.4.4 发布时,所有可用的 Java 8 运行时环境都标识自己为版本 1.8。而当前的 OpenJDK Java 8 标识自己为版本 8。因此,XStream 1.4.4 不知道这个版本支持什么,于是退回到纯 Java 模式,这种模式无法创建没有默认构造函数的对象。这就是为什么存在 XStream 1.4.20 的原因……
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.4</version>
</dependency>
经过实验1.4.10就可以解决这个问题于是可以通过配置dependencies
去避免这个报错,同样可以解决问题
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
</dependencies>
<configuration>
<warName>openjdk1.8-war2.5</warName>
<useCache>true</useCache>
</configuration>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>