jenkins上的maven grunt jasmine前端测试集成

时间:2016-11-07 12:11:03

标签: maven jenkins gruntjs jasmine aem

我目前正在尝试在我们的Jenkins服务器上集成maven grunt jasmine。 期望的结果是,当我们运行构建时(在配置中我们将运行mvn clean install -Pgrunt)然后jenkins job运行所有前端茉莉花测试。

这是我的项目pom.xml

<profiles>
    <profile>
        <id>install-node</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>1.1</version>
                    <executions>
                        <execution>
                            <id>install node and npm</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>npm install</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <nodeVersion>v4.6.1</nodeVersion>
                        <npmVersion>2.15.9</npmVersion>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>grunt</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>1.1</version>
                    <executions>
                        <execution>
                            <id>grunt build</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>grunt</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
pom旁边的p是我的package.json

{
  "name":"frontend-tools",
  "version":"0.0.1",
  "dependencies": {
    "grunt": "~0.4.5",
    "grunt-cli": "~0.1.13",
    "grunt-contrib-jshint":"~0.10.0",
    "grunt-contrib-jasmine":"~1.0.3"
  },
  "devDependencies": {}
}

和Gruntfile.js

module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-contrib-jshint');

  grunt.initConfig({
    jasmine: {
      customTemplate: {
        src: 'jcr_root/apps/example/components/**/*.js',
        options: {
            specs: 'jcr_root/apps/example/components/**/spec/*.js',
            helpers: 'jcr_root/apps/example/components/**/spec/*Helper.js',
            vendor: [
                      "jcr_root/etc/designs/example/clientlibs_tools/js/jquery.js"
                    ]
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jasmine');
  grunt.registerTask('default', ['jasmine']);

};

我在component / ** / spec文件夹下的某个地方有一个虚拟测试,说它是一个dummytest.js:

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

当我在本地运行时,一切正常,mvn clean install -Pgrunt 它给了我这样的预期效果:

[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ app ---
[INFO] Deleting Z:\CQ5\example\app\target
[INFO]
[INFO] --- frontend-maven-plugin:1.1:grunt (grunt build) @ app ---
[INFO] Running 'grunt ' in Z:\CQ5\example\app
[INFO] Running "jasmine:customTemplate" (jasmine) task
[INFO]  A suite
[INFO]    - contains spec with an expectation......â??
[INFO]  A suite is just a function
[INFO]    - and so is a spec......â??
[INFO] 4 specs in 0.002s.
[INFO] >> 0 failures
[INFO]
[INFO] Done, without errors.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

但是当我基于完全相同的代码和配置在jenkins上构建作业时,它变成:

[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ app ---
[INFO] 
[INFO] --- frontend-maven-plugin:1.1:grunt (grunt build) @ app ---
[INFO] Running 'grunt ' in /data/apps/jenkins/workspace/maven-grunt-jasmine-testrun/app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.627 s
[INFO] Finished at: 2016-11-07T11:26:39+00:00
[INFO] Final Memory: 20M/218M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.eirslett:frontend-maven-plugin:1.1:grunt (grunt build) on project app: Failed to run task: 'grunt ' failed. java.io.IOException: Cannot run program "/data/apps/jenkins/workspace/maven-grunt-jasmine-testrun/app/node/node" (in directory "/data/apps/jenkins/workspace/maven-grunt-jasmine-testrun/app"): error=2, No such file or directory -> [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/MojoFailureException
Waiting for Jenkins to finish collecting data

这是我的jenkins配置: enter image description here

正如您所看到的,我正在尝试测试我的节点npm和grunt是否正确安装,这是输出信息:

+ cd app
+ pwd
/data/apps/jenkins/workspace/maven-grunt-jasmine-testrun/app
+ npm install
npm WARN frontend-tools@0.0.1 No description
npm WARN frontend-tools@0.0.1 No repository field.
npm WARN frontend-tools@0.0.1 No license field.
+ grunt --help
/tmp/hudson5273188301428948376.sh: line 2: grunt: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

所以我只是想知道,以前有没有专家经历过这种配置设置?任何提示或建议都会很棒。 感谢

1 个答案:

答案 0 :(得分:0)

经过研究,我发现它实际上是node_modules文件夹已过时,所以我创建了另一个配置文件只是为了删除jenkins上的node_modules。
这是更新的pom.xml配置文件:

<profiles>
    <profile>
        <id>install-node</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>1.1</version>
                    <executions>
                        <execution>
                            <id>install node and npm</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>npm install</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <nodeVersion>v4.6.1</nodeVersion>
                        <npmVersion>2.15.9</npmVersion>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>remove-node-module</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <followSymLinks>false</followSymLinks>
                        <filesets>
                            <fileset>
                                <directory>${basedir}/node_modules</directory>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>grunt-jenkins</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>1.1</version>
                    <executions>
                        <execution>
                            <id>grunt jenkins</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>grunt</goal>
                            </goals>
                            <configuration>
                                <arguments>jenkins</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

所以,在jenkins上,运行mvn clean install -Pinstall-node来安装npm 然后,mvn clean test -Pgrunt-jenkins,
如果失败,请运行mvn clean test -Premove-node-module以删除过时的node_modules 终于再次运行mvn clean test -Pgrunt-jenkins它为我构建了一个可靠的东西。感谢