自此更新以来已经有几年了,并且提供的解决方案无法从全新安装中运行。经过一番摆弄,这是我想出的:
如上所述,我使用 frontend-maven-plugin 安装 Node、NPM、Grunt 和 Babel。
Node 和 NPM 下载/安装 Grunt 和 babel。
咕噜呼唤巴别塔
Babel 进行转译。
Maven
要开始此过程,请将其添加到 pom.xml 的构建部分
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>initialize</phase>
</execution>
<execution>
<id>npm</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
<phase>initialize</phase>
</execution>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
<configuration>
<nodeVersion>v10.4.1</nodeVersion>
<npmVersion>6.1.0</npmVersion>
</configuration>
</plugin>
<!--- rest of your plugins follow -->
NPM
在与 pom.xml 相同的项目级别,创建一个 package.json 文件。该文件告诉 Node/NPM 它需要安装到 node_modules 目录中的依赖项
{
"name": "npm-maven-base-project",
"version": "0.0.1",
"description": "Install Grunt and Babel to do some ES6 to ES5 transpiling",
"devDependencies": {
"@babel/core": "^7.0.0-beta.54",
"@babel/preset-env": "^7.0.0-beta.54",
"grunt-babel": "8.0.0-beta.0",
"grunt": "~0.4.5",
"grunt-cli": "1.2.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0",
"load-grunt-tasks": "^3.5.2"
},
"main": "GruntFile.js",
"dependencies": {
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
咕噜声
接下来我们需要告诉 grunt 它需要做什么。当执行时,Grunt 会调用 Babel 来编译你的代码。因此,在与 pom.xml 和 package.json 相同的目录中创建一个“Gruntfile.js”。
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt); // npm install --save-dev
// load-grunt-tasks
grunt.initConfig({
babel : {
options : {
sourceMap : false
},
dist : {
files : [{
'src/main/webapp/resources/scripts/destination.js' : 'src/main/webapp/resources/scripts/source.es6'
},{
'src/main/webapp/resources/scripts/destination.js' : 'src/main/webapp/resources/scripts/source.es6'
}]
}
}
});
grunt.registerTask('default', [ 'babel' ]);
};
上面的“文件”条目数组列出了您要转译的所有文件。它的格式是 {'destination' : 'source'}。注意:没有防止意外覆盖的标志。
注意:当你最终运行 mvn install 时,如果你收到这个错误信息......
警告:“路径”参数必须是字符串类型。收到类型未定义使用--force继续
....表示源文件路径拼写错误或目的地不可写
通天塔
最后,我们需要告诉 Babel 我们希望它从 ES6 转换到 ES5。为此,请在与“pom.xml”、“package.json”和“Gruntfile.js”相同的目录中创建一个“.babelrc”文件。用这个填充它:
{
"presets": ["@babel/preset-env"]
}
现在只要交叉你的手指并运行......
mvn 安装
...您的构建应该运行并转译您的 JS。