如何在 Maven 中使用 Groovy 插件访问变量
How access a variable with Groovy Plugin in Maven
我有一个在子模块中包含 React 的应用程序,我想使用存在于父模块中的 node.exe。
在父 POM 中,我定义了一个变量,其路径为 node.exe
<properties>
<project.name>Corretor Online</project.name>
<nodeBase>C:/col/COL</nodeBase>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Obs.: 我想在 nodeBase 变量中使用 ${project.basedir}
并且我尝试在子 POM 中访问这些变量以作为 ProcessBuilder 方法中的参数传递:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<id>webpack</id>
<phase>process-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source><![CDATA[
println "Compiling client code..."
def webpack = new ProcessBuilder(["" + nodeBase + "/node/node", "" + nodeBase + "/node_modules/webpack/bin/webpack.js", "-p", "--progress", "--bail"]).inheritIO().directory(project.getBasedir())
def env = webpack.environment()
env.put("WAR_NAME", project.build.finalName)
def proc_webpack = webpack.start()
proc_webpack.waitForOrKill(120000)
if(proc_webpack.exitValue() != 0)
throw new org.apache.maven.plugin.MojoFailureException("Error compiling client code")
]]></source>
</configuration>
</execution>
</executions>
</plugin>
但是我收到这个错误:
[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute (webpack) on project col-backoffice-war: groovy.lang.MissingPropertyException: No such property: nodeBase for class: script1539714060887 -> [Help 1]
如果我从 "nodeBase" 更改为“${nodeBase},我会收到此错误:
[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute (webpack) on project col-backoffice-war: startup failed, script1539714572018.groovy: 2: unexpected char: '#' @ line 2, column 134.
但是,如果我输入文字路径,我可以构建。
def webpack = new ProcessBuilder(["C:/col/COL/node/node",
"C:/col/COL/node_modules/webpack/bin/webpack.js", "-p",
"--progress",
"--bail"]).inheritIO().directory(project.getBasedir())
我找不到解决方案,你能帮我看看如何访问这个变量吗?
非常感谢!
我发现了问题。
需要将参数放在引号内,如下例
def webpack = new ProcessBuilder("${nodeBase}" + "/node/node", "C:/col/COL/node_modules/webpack/bin/webpack.js", "-p", "--progress", "--bail"]).inheritIO().directory(project.getBasedir())
我有一个在子模块中包含 React 的应用程序,我想使用存在于父模块中的 node.exe。
在父 POM 中,我定义了一个变量,其路径为 node.exe
<properties>
<project.name>Corretor Online</project.name>
<nodeBase>C:/col/COL</nodeBase>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Obs.: 我想在 nodeBase 变量中使用 ${project.basedir}
并且我尝试在子 POM 中访问这些变量以作为 ProcessBuilder 方法中的参数传递:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<id>webpack</id>
<phase>process-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source><![CDATA[
println "Compiling client code..."
def webpack = new ProcessBuilder(["" + nodeBase + "/node/node", "" + nodeBase + "/node_modules/webpack/bin/webpack.js", "-p", "--progress", "--bail"]).inheritIO().directory(project.getBasedir())
def env = webpack.environment()
env.put("WAR_NAME", project.build.finalName)
def proc_webpack = webpack.start()
proc_webpack.waitForOrKill(120000)
if(proc_webpack.exitValue() != 0)
throw new org.apache.maven.plugin.MojoFailureException("Error compiling client code")
]]></source>
</configuration>
</execution>
</executions>
</plugin>
但是我收到这个错误:
[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute (webpack) on project col-backoffice-war: groovy.lang.MissingPropertyException: No such property: nodeBase for class: script1539714060887 -> [Help 1]
如果我从 "nodeBase" 更改为“${nodeBase},我会收到此错误:
[ERROR] Failed to execute goal org.codehaus.groovy.maven:gmaven-plugin:1.0:execute (webpack) on project col-backoffice-war: startup failed, script1539714572018.groovy: 2: unexpected char: '#' @ line 2, column 134.
但是,如果我输入文字路径,我可以构建。
def webpack = new ProcessBuilder(["C:/col/COL/node/node",
"C:/col/COL/node_modules/webpack/bin/webpack.js", "-p",
"--progress",
"--bail"]).inheritIO().directory(project.getBasedir())
我找不到解决方案,你能帮我看看如何访问这个变量吗?
非常感谢!
我发现了问题。
需要将参数放在引号内,如下例
def webpack = new ProcessBuilder("${nodeBase}" + "/node/node", "C:/col/COL/node_modules/webpack/bin/webpack.js", "-p", "--progress", "--bail"]).inheritIO().directory(project.getBasedir())