我遵循相同的教程并停留在同一点:frontend-maven-plugin 似乎无法正常工作。此外,作者提供的代码库在我看来组织得不好,例如两个pom.xml 文件,一个在项目根文件夹,另一个在基本文件夹。
这是我想出来的,终于奏效了:
- 在
pom.xml 中添加frontend-maven-plugin 在plugins 以及您希望它执行的脚本。确保包含版本标签并提供最新版本(您可以在Maven Repository 中找到版本信息),否则将无法解析插件。
<build>
<plugins>
<!-- For the sake of simplicity, -->
<!-- other plugins are omitted. -->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.0</version>
<configuration>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v14.17.3</nodeVersion>
<npmVersion>7.18.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>webpack build</id>
<goals>
<goal>webpack</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 在项目根文件夹中创建
package.json文件,即与pom.xml相同的位置并粘贴以下内容:
{
"name": "spring-data-rest-and-reactjs",
"version": "0.1.0",
"description": "Demo of ReactJS + Spring Data REST",
"repository": {
"type": "git",
"url": "git@github.com:spring-guides/tut-react-and-spring-data-rest.git"
},
"keywords": [
"rest",
"hateoas",
"spring",
"data",
"react"
],
"author": "Greg L. Turnquist",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/spring-guides/tut-react-and-spring-data-rest/issues"
},
"homepage": "https://github.com/spring-guides/tut-react-and-spring-data-rest",
"dependencies": {
"react": "^16.5.2",
"react-dom": "^16.5.2",
"rest": "^1.3.1"
},
"scripts": {
"watch": "webpack --watch -d --output ./target/classes/static/built/bundle.js"
},
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0"
}
}
- 在项目根文件夹中创建
webpack.config.js文件,即与pom.xml相同的位置并粘贴以下内容:
var path = require('path');
module.exports = {
entry: './src/main/js/app.js',
devtool: 'sourcemaps',
cache: true,
mode: 'development',
output: {
path: __dirname,
filename: './src/main/resources/static/built/bundle.js'
},
module: {
rules: [
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
use: [{
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}]
}
]
}
};
-
将同一目录中的client.js 和整个api 文件夹复制到您的项目中。 app.js 使用 client.js,它使用 api 文件夹中的两个 .js 文件。这三个文件的源码可以在the tutorial repository找到。
-
现在您应该拥有一切以使 React 在这个项目中工作。虽然取决于您使用的 IDE,但它可能仍然“不工作”。让我进一步解释一下我的意思:
- 如果您使用命令行运行项目,只需在项目根文件夹中键入
./mvnw spring-boot:run,它应该会像一个魅力一样工作。
- 如果您像我一样使用绿色小三角形在 IntelliJ 中运行应用程序,您必须编辑运行/调试配置才能使其工作;否则应用程序可以成功运行,但不会生成任何
bundle.js:
首先,打开 IntelliJ -> 运行 -> 编辑配置
其次,点击Before Launch部分中的+按钮并选择Run Maven Goal
第三,在命令行中输入generate-resources,点击确定。我们选择frontend-maven-plugin git repo 中提到的这个命令,如果不指定,目标在generate-resources 阶段执行。
最后,您可以创建一个 Maven 目标以在构建之前清理所有内容。这是Before launch 的样子:
现在单击绿色三角形运行应用程序,您将获得与教程中提到的相同的结果(除了员工列表中没有寄宿生,因为我们没有.css 文件)。
一些最后的想法:
- 您添加
frontend-maven-plugin 以在您的项目中使用node 和npm。
- 您添加
package.json 以安装必要的 JavaScript 模块,例如React.js。
- 您添加
webpack.config.js 以指示webpack 将javascript 文件转换为一个名为bundle.js 的文件并将其输出到项目的静态文件夹中
- 您在
pom.xml 中添加了许多execution 和goal 标签以自动执行第2 步和第3 步。
- 您执行 maven 目标以启动自动化过程。