1. 程式人生 > >Maven開源外掛(yuicompressor-maven)編譯報錯 A required class is missing:org.mozilla.javascript.ErrorReporter

Maven開源外掛(yuicompressor-maven)編譯報錯 A required class is missing:org.mozilla.javascript.ErrorReporter

 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.遺留問題

為什麼相同的配置本地可以,伺服器主機上就是不行,待後續再研究!