【问题标题】:Run "npm install" + test script via Maven for Node.js project通过 Maven 为 Node.js 项目运行“npm install”+ 测试脚本
【发布时间】:2017-09-07 12:40:45
【问题描述】:

作为我们 Jenkins 构建的一部分,Jenkins 使用 Maven,而后者又在我们项目的根目录中使用 pom.xml 文件。现在几乎没有任何操作:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dqsalpha-dev.app</groupId>
    <artifactId>dqsalpha</artifactId>
    <version>1</version>
</project>

我想在这个 pom 文件中添加一件事 - 我想在运行 maven build 时运行一个测试(它只是一个 shell 脚本)。

通过 pom 文件向 Maven 构建添加测试的最简单方法是什么?

类似这样的:

  <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.dqsalpha-dev.app</groupId>
        <artifactId>dqsalpha</artifactId>
        <version>1</version>
        <test>
          <bash>
            @test.sh
          </bash>
        </test>
   </project>

除了这完全不正确。我只是意识到我可能还需要使用 npm install 安装 Node.js 依赖项。

我试过了:

https://bitbucket.org/atlassian/bash-maven-plugin

然后我的 pom.xml 看起来像:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dqsalpha-dev.app</groupId>
    <artifactId>dqsalpha</artifactId>
    <version>1</version>
    <build>
        <plugins>
            <plugin>

                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>bash-maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <id>test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <script>
                        npm install;
                        ./test.sh
                    </script>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <example.one>Variable replacement is available from Maven.</example.one>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.atlassian.maven.plugins</groupId>
            <artifactId>bash-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

然后我得到这个错误:

$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building dqsalpha 1
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.atlassian.maven.plugins:bash-maven-plugin:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.187 s
[INFO] Finished at: 2017-04-11T16:09:24-07:00
[INFO] Final Memory: 8M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin com.atlassian.maven.plugins:bash-maven-plugin:1.0-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact com.atlassian.maven.plugins:bash-maven-plugin:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

【问题讨论】:

    标签: java bash shell maven jenkins


    【解决方案1】:

    我只是意识到我可能还需要安装 Node.js 依赖项,使用 npm install。

    如果您在 test.sh 中执行的操作是 Node.js 和 npm 的东西,您可以改用 frontend-maven-plugin,尽管它的名称是,它旨在安装和运行 Node.js 和 npm。这是一个示例,但请参阅https://github.com/eirslett/frontend-maven-plugin 了解更多信息。

    <plugin>
      <groupId>com.github.eirslett</groupId>
      <artifactId>frontend-maven-plugin</artifactId>
      <version>1.9.1</version>
    
      <configuration>
        <nodeVersion>v12.16.0</nodeVersion>
      </configuration>
    
      <executions>
        <execution>
          <id>Install Node.js and npm</id>
          <goals>
            <goal>install-node-and-npm</goal>
          </goals>
        </execution>
    
        <execution>
          <id>npm install</id>
          <goals>
            <goal>npm</goal>
          </goals>
          <configuration>
            <workingDirectory>${your.npm.script.directory}</workingDirectory>
            <arguments>install</arguments>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    【讨论】:

      【解决方案2】:

      你可以使用exec-maven-plugin:

      <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <executions>
              <execution>
                  <id>test1</id>
                  <phase>package</phase>
                  <goals>
                      <goal>exec</goal>
                  </goals>
                  <configuration>
                      <executable>${basedir}/test.sh</executable>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 2021-02-12
        • 1970-01-01
        • 1970-01-01
        • 2016-09-13
        • 2019-05-21
        • 2011-11-27
        • 1970-01-01
        • 2021-09-04
        相关资源
        最近更新 更多