带有npm install的exec-maven-plugin

时间:2018-08-14 11:13:52

标签: maven maven-plugin

我尝试在我的项目中使用exec-maven-plugin。

这是代码

            <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <version>1.6.0</version>
          <executions>
            <execution>
              <id>npm install</id>
              <goals>
                <goal>exec</goal>
              </goals>
              <phase>generate-resources</phase>
              <configuration>
                <executable>${node.directory}/${npm.executable}</executable>
                <arguments>
                  <argument>install</argument>
                </arguments>
                <workingDirectory>src/main/angular2/tourism</workingDirectory>
                <target>
                  <echo message="Npm install" />
                </target>
              </configuration>
            </execution>
          </executions>
        </plugin>

这是我的目录结构 enter image description here

提到的错误是

/Users/admin/Application-Marwen/workspace/Tourism/Tourism-Web/node/npm: line 34: node: command not found

你能帮我吗

我使用知道的frontend-maven-plugin

这是代码

            <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <workingDirectory>src/main/angular5/tourism</workingDirectory>
                <!-- where to install npm -->
                <installDirectory>src/main/angular5/tourisml</installDirectory>
            </configuration>                
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <nodeVersion>v8.11.3</nodeVersion>
                        <npmVersion>6.3.0</npmVersion>                      
                    </configuration>                        
                </execution>                  
                <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <!-- Optional configuration which provides for running any npm command -->
                        <configuration>
                            <arguments>install</arguments>
                            <workingDirectory>src/main/angular5/tourism</workingDirectory>
                        </configuration>
                 </execution>
                 <execution>
                     <id>prod</id>
                     <goals>
                       <goal>npm</goal>
                     </goals>
                     <configuration>
                       <arguments>run-script build</arguments>
                       <workingDirectory>src/main/angular5/tourism</workingDirectory>
                     </configuration>
                     <phase>generate-resources</phase>
                  </execution>                                                      
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}-${project.version}.zip</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>cfg-main-resources</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>${basedir}/src/main/angular5/tourism/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

还有assembly.xml文件

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>distribution</id>
<formats>
    <format>zip</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
    <fileSet>
        <outputDirectory>statics</outputDirectory>
        <directory>dist</directory>
        <includes>
            <include>**/*</include>
        </includes>
    </fileSet>
</fileSets>

目标是准备一个包含已编译代码的zip文件,以将其部署到服务器上

这是获得的目录结构

enter image description here

1)我不明白为什么dist文件夹不包含编译后的代码(我进一步想知道它必须包含什么内容) 2)节点和npm安装在目录tourisml / node中,为什么 3)出现错误,指出

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (cfg-main-resources) on project tourism-web: Failed to create assembly: Error creating assembly archive distribution: You must set at least one file. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (cfg-main-resources) on project tourism-web: Failed to create assembly: Error creating assembly archive distribution: You must set at least one file.

通常我应该获得一个zip文件($ {project.artifactId}-$ {project.version} .zip),并且我认为它失败了,因为其中没有文件。这是正确的吗?

3 个答案:

答案 0 :(得分:1)

建议您不要使用frontend-maven-plugin来使用exec-maven-plugin来构建Node源。

使用该插件,您不需要在计算机上安装npm installnpm:该插件可以自行管理所有内容,因此您不会遇到类似node的问题。像下面这样的配置应该可以工作:

command not found

答案 1 :(得分:0)

最后,我使用了frontend-maven-plugin

这是pom.xml的摘录

        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <workingDirectory>src/main/angular5/tourism</workingDirectory>
                <!-- where to install npm -->
                <installDirectory>temp</installDirectory>
            </configuration>                
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <nodeVersion>v8.11.3</nodeVersion>
                        <npmVersion>6.3.0</npmVersion>                      
                    </configuration>                        
                </execution>
                <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <!-- Optional configuration which provides for running any npm command -->
                        <configuration>
                            <arguments>install</arguments> 
                        </configuration>
                 </execution>
                 <execution>
                     <id>prod</id>
                     <goals>
                       <goal>npm</goal>
                     </goals>
                     <configuration>
                       <arguments>run-script build</arguments>
                     </configuration>
                     <phase>generate-resources</phase>
                  </execution>                                                      
            </executions>
        </plugin>

谢谢您的回答

答案 2 :(得分:0)

我已安装节点并将已安装的文件夹移至$ {project.baseUri} / node /下。

该错误现在已解决,我可以使用命令提示符中的以下命令进行npm安装。

c:\main\prjt\apphost_prjt>mvn frontend:npm