Maven开源插件(yuicompressor-maven)编译报错 A required class is missing:org.mozilla.javascript.ErrorReporter

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/posonrick/article/details/81045111

 1.报错现象       

          这两天遇到一个Maven插件(JS压缩)在公司Linux主机编译出错的问题,出错内容如下:

[ERROR] Failed to execute goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.0:compress (default) on project crm-web: Execution default of goal net.alchim31.maven:yuicompressor-maven-plugin:1.5.0:compress failed: A required class was missing while executing net.alchim31.maven:yuicompressor-maven-plugin:1.5.0:compress: org/mozilla/javascript/ErrorReporter
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>net.alchim31.maven:yuicompressor-maven-plugin:1.5.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/data/crm/crmweb/.m2/repository/net/alchim31/maven/yuicompressor-maven-plugin/1.5.0/yuicompressor-maven-plugin-1.5.0.jar
[ERROR] urls[1] = file:/data/crm/crmweb/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------: org.mozilla.javascript.ErrorReporter

2.使用场景

用此插件主要用于对JS进行压缩,以提高用户对系统的攻击门槛,因此我在我们WEB工程的pom增加了如下配置:

<plugin>
			<groupId>net.alchim31.maven</groupId>
			<artifactId>yuicompressor-maven-plugin</artifactId>
			<version>1.5.01</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>compress</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<encoding>UTF-8</encoding>
				<jswarn>false</jswarn>
				<nosuffix>true</nosuffix>
				<force>true</force>
				<linebreakpos>-1</linebreakpos>
				<sourceDirectory>WebRoot</sourceDirectory>
				<outputDirectory>target/crm-web-3.0.0-SNAPSHOT</outputDirectory>
				<includes>
					<include>js/crmpub/compress/js/security_check.js</include>
				</includes>
				<excludes>
				</excludes>
			</configuration>
		</plugin>

        增加之后本地可以正常使用并可以达到预期效果,而在公司Linux服务器上确报第一节中的错误。

3.处理过程

        由于公司主机是在内网网络上不通,所以先将插件需要的一些第三方jar导入到了公司私服,各种导入之后,仍然不行;于是将插件源码下载下来,发现有些JAR包冲突,然后很开心的把冲突都解决了,编译打包到私服,仍然报错;然后各种关键字百度,网上各种说法试过了都不行,后经过尝试以下两种方法确定可以解决:

 
 

4.可行方法

4.1.服务器安装此插件

        各种方法尝后,把源码放到公司主机上编译安装了一下,命令如下:

mvn clean package install -Dmaven.test.skip=true

        再试了一下竟然编译WEB项目成功,并成功压缩了JS文件,有点喜出望外的感觉,必竟整整折腾了一个下午。不过原因真的没法有搞明白,直到现在也没有明白!

4.2.改插件引用配置

虽然上面的方法已经解决,但是心理还是不舒服,以后其它地方编译,也需要将源码拿到对应的主机上编译一次,比较不便与繁锁,于是继续上其它网站找其它解决方法,终于在https://stackoverflow.com上找到了对应帮助。主要参考了以下两启遍文章:

https://stackoverflow.com/questions/3819985/yui-compressor-maven-a-required-class-is-missing-org-mozilla-javascript-errorr

https://stackoverflow.com/questions/6652550/yui-compressor-stringindexoutofboundsexception-on-jboss

最终的配置如下,红色部分是相对之前增加的部分:

<plugin>
			<groupId>net.alchim31.maven</groupId>
			<artifactId>yuicompressor-maven-plugin</artifactId>
			<version>1.5.01</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>compress</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<encoding>UTF-8</encoding>
				<jswarn>false</jswarn>
				<nosuffix>true</nosuffix>
				<force>true</force>
				<linebreakpos>-1</linebreakpos>
				<sourceDirectory>WebRoot</sourceDirectory>
				<outputDirectory>target/crm-web-3.0.0-SNAPSHOT</outputDirectory>
				<includes>
					<include>js/crmpub/compress/js/security_check.js</include>
				</includes>
				<excludes>
				</excludes>
			</configuration>
			<dependencies>
  				<dependency>
					<groupId>org.sonatype.plexus</groupId>
					<artifactId>plexus-build-api</artifactId>
					<version>0.0.7</version>
  				</dependency>
  				<dependency>
    				<groupId>org.codehaus.plexus</groupId>
    				<artifactId>plexus-utils</artifactId>
					<version>3.0.15</version>
  				</dependency>
  				<dependency>
    				<groupId>com.yahoo.platform.yui</groupId>
    				<artifactId>yuicompressor</artifactId>
    				<version>2.4.7</version>
    				<exclusions>
      					<exclusion>
         					<groupId>rhino</groupId>
         					<artifactId>js</artifactId>
      					</exclusion>
    				</exclusions>
				</dependency>
  				<dependency>
    				<groupId>rhino</groupId>
    				<artifactId>js</artifactId>
    				<scope>compile</scope>
    				<version>1.7R2</version>
  				</dependency>
			</dependencies>
		</plugin>

        其最终原因是因为多个rhino包造成的影响,因此,直接在插件将插件需要用到的包在其下面依赖进来,其实插件源码pom已经有这些引用,但就是在主机上不行。另外,还有一点要注意rhino包需要yuicompress包之后。

5.遗留问题

为什么相同的配置本地可以,服务器主机上就是不行,待后续再研究!

猜你喜欢

转载自blog.csdn.net/posonrick/article/details/81045111
今日推荐