frontend-maven-plugin插件問題解決


1.插件介紹

frontend-maven-plugin為項目本地下載/安裝Node和NPM,運行npm install命令 。
它適用於Windows,OS X和Linux。
這個插件也可以下載Node和Yarn,
然后運行yarn install你的項目。

使用這個插件目的:

  • 讓你的前端和后端版本盡可能分開,通過減少它們之間的交互量到最低限度; 僅使用1個插件。
  • 讓您在構建過程中使用Node.js及其庫,而無需為構建系統全局安裝Node / NPM
  • 讓您確保在每個構建環境中運行的Node和NPM的版本是相同的

2.插件使用示例

我所在的項目security-admin/pom.xml配置如下:

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <workingDirectory>${project.build.directory}</workingDirectory>
    </configuration>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v8.12.0</nodeVersion>
                <npmVersion>6.4.1</npmVersion>
            </configuration>
        </execution>
        <execution>
            <phase>prepare-package</phase>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>                
            </configuration>
        </execution>
    </executions>
</plugin>

3.離線環境打包問題解決方法

該插件在打包security-admin工程時,
要求聯網下載文件,並且之后的npm需要連接遠程倉庫,
而我們的工程是在內網打包的,不能隨意連接外網,
從而會導致打包失敗。

使用的打包測試命令:
mvn package -pl security-admin -am -DskipTests

離線打包的解決方法有兩種:

3.1.修改downloadRoot改變下載的地址,使用國內或者內網URL:

下載node-v8.12.0-linux-x64.tar.gz安裝包,默認連接如下網址:

<!-- optional: where to download node and npm from. Defaults to https://nodejs.org/dist/ -->
<downloadRoot>https://nodejs.org/dist/</downloadRoot>
https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.gz

3.1.1.如果無法連接國外網址,可以使用國內淘寶的網址:

<downloadRoot>http://npm.taobao.org/mirrors/node/</downloadRoot>
http://npm.taobao.org/mirrors/node/v8.12.0/node-v8.12.0-linux-x64.tar.gz

downloadRoot的配置參考如下:

<execution>
    <phase>prepare-package</phase>
    <id>install node and npm</id>
    <goals>
        <goal>install-node-and-npm</goal>
    </goals>
    <configuration>
        <nodeVersion>v8.12.0</nodeVersion>
        <npmVersion>6.4.1</npmVersion>
        <downloadRoot>http://npm.taobao.org/mirrors/node/</downloadRoot>
    </configuration>
</execution>

注意downloadRoot不支持本地文件目錄

3.1.2.如果國內網址也無法使用,只能連接內網

可以使用本地啟動tomcat的方式,將要下載的安裝包提前下好,
放到tomcat的webapp目錄下,啟動tomcat提供下載服務,
詳細步驟請參考:
使用tomcat搭建HTTP文件下載服務器
然后將downloadRoot改為tomcat服務的路徑,從tomcat下載相應的包,解決問題。

3.2.使用本地目錄緩存,則不需要聯網下載

查看打包過程發現如下日志:

[INFO] Downloading https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.gz to /home/maven/repository/com/github/eirslett/node/8.12.0/node-8.12.0-linux-x64.tar.gz

插件從指定網址下載node-v8.12.0-linux-x64.tar.gz下載文件到本地緩存目錄后,
並且改名為node-8.12.0-linux-x64.tar.gz,
當每次開始編譯時,插件會先查看緩存目錄文件是否已經存在,
存在則不會重新下載,不存在才會去聯網,
所有可以先從其他路徑獲取需要的安裝包文件,
手動把node-v8.12.0-linux-x64.tar.gz放到對應的目錄:
/home/maven/repository/com/github/eirslett/node/8.12.0/

具體操作如下:
下載安裝包node-v8.12.0-linux-x64.tar.gz:
https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.gz
創建目錄:
mkdir -p /home/maven/repository/com/github/eirslett/node/8.12.0/
上傳安裝包:
ftp node-v8.12.0-linux-x64.tar.gz /home/maven/repository/com/github/eirslett/node/8.12.0/
並且改名,去掉名字中的v:
mv node-v8.12.0-linux-x64.tar.gz node-8.12.0-linux-x64.tar.gz

上面需要的npm進行同樣的操作即可:

[INFO] Downloading https://registry.npmjs.org/npm/-/npm-6.4.1.tgz to /home/maven/repository/com/github/eirslett/npm/6.4.1/npm-6.4.1.tar.gz
mkdir -p /home/maven/repository/com/github/eirslett/npm/6.4.1/
mv npm-6.4.1.tgz npm-6.4.1.tar.gz

3.3.npm使用內網的制品庫作為遠程倉庫地址

在pom中插件的配置項中增加如下兩個配置項:
vim security-admin/pom.xml

<npmRegistryURL>https://10.41.103.97:443/artifactory/api/npm/zenap-npm-virtual</npmRegistryURL>
<arguments>install --strict-ssl=false</arguments>

3.3.1.如果未 設置npmRegistryURL則會報如下錯誤,因為內網無法訪問到外網遠程倉庫:

[INFO] --- frontend-maven-plugin:1.6:npm (npm install) @ security-admin-web ---
[INFO] Running 'npm install' in /home/compile/ranger/security-admin/target
[ERROR] npm ERR! code ENOTFOUND
[ERROR] npm ERR! errno ENOTFOUND
[ERROR] npm ERR! network request to https://registry.npmjs.org/requirejs failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
[ERROR] npm ERR! network This is a problem related to network connectivity.
[ERROR] npm ERR! network In most cases you are behind a proxy or have bad network settings.
[ERROR] npm ERR! network
[ERROR] npm ERR! network If you are behind a proxy, please make sure that the
[ERROR] npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
[ERROR]
[ERROR] npm ERR! A complete log of this run can be found in:
[ERROR] npm ERR!     /root/.npm/_logs/2018-10-15T08_23_03_167Z-debug.log
[INFO] -------------------------------------------------------------------

3.3.2.改為內網的倉庫后,還是報錯,無法解析返回的html文件:

17 http fetch GET 200 https://10.41.103.97:443/artifactory/public-npm-remote-cache/requirejs 64ms
18 silly fetchPackageMetaData error for requirejs@^2.3.6 Unexpected token < in JSON at position 0 while parsing near '<!DOCTYPE HTML PUBLI...'

查看淘寶的npm返回結果,返回的是一個json文件,里面記錄了版本信息:
https://registry.npm.taobao.org/requirejs

查看資料后,發現如下URL會返回josn格式的結果:
https://10.41.103.97:443/artifactory/api/npm/zenap-npm-virtual/requirejs

所以npmRegistryURL配置如下:
<npmRegistryURL>https://10.41.103.97:443/artifactory/api/npm/zenap-npm-virtual</npmRegistryURL>

3.3.3.如果不配置設置strict-ssl為false,則打包時會報證書不存在錯誤:

23 verbose stack FetchError: request to https://10.41.103.97:443/artifactory/api/npm/zenap-npm-virtual/requirejs failed, reason: Hostname/IP doesn't match certificate's altnames: "IP: 10.41.103.97 is not in the cert's list: "

設置strict-ssl為false關閉強制SSL檢查:
<arguments>install --strict-ssl=false</arguments>
實際上這個參數執行一次就會改變全局變量,
但是重復執行也沒有問題,可以寫入到pom中執行,
防止第一次編譯的時候沒有該配置項導致失敗。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM