Java+maven+selenium+testng+jenkins自动化环境搭建(补充)
阅读原文时间:2021年04月20日阅读:1

原来配置的过程地址:

http://blog.csdn.net/cyjs1988/article/details/72780982

http://blog.csdn.net/taoxu858/article/details/71107183

在配置环境中遇到的问题:

1. 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案:

编译maven工程的时候出现如下错误:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1

pom中如下配置maven插件,配置中声明使用JDK1.8:

[html]  view plain  copy

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.1</version>
  5. <configuration>
  6. <verbose>true</verbose>
  7. <fork>true</fork>
  8. <executable>${JAVA8_HOME}/bin/javac</executable>
  9. </configuration>
  10. </plugin>

这里的${JAVA8_HOME}这个变量是在settings.xml中配置的,如下:

[html]  view plain  copy

  1. <profile>
  2. <id>custom-compiler</id>
  3. <properties>
  4. <JAVA8_HOME>C:\Program Files (x86)\Java\jdk1.8.0_73</JAVA8_HOME>
  5. </properties>
  6. </profile>

当然这里应该需要激活,所以settings.xml文件还应该有如下配置:

[html]  view plain  copy

  1. <activeProfiles>
  2. <activeProfile>custom-compiler</activeProfile>
  3. </activeProfiles>

从pom文件中CTRL点击变量JAVA8_HOME能跳到settings.xml中找到它的定义处,按理来说应该是能找到这个变量,出现上述问题并不是因为找不到这个变量。我将pom文件中的JAVA8_HOME这个变量直接用实际的路径替换,即替换为

[html]  view plain  copy

  1. C:\Program Files (x86)\Java\jdk1.8.0_73\bin\javac

发现编译通过,这就奇怪了。

揭晓原因:

maven其实是有一个默认的仓库.m2仓库和默认的settings.xml配置文件,我们在这个默认的settings.xml文件中也添加了一个JAVA8_HOME的变量后,编译就通过了,这就说明,maven编译的时候找的不是我在idea中配置的我自定义的settings.xml,而是先找的它默认的那个。因为里面没有,所以之前找不到JAVA8_HOME,导致编译失败、

总结:maven编译的时候应该是先找的默认的settings.xml,如果找不到,才会去找我在idea的settings选项下配置的“User settings file”中配置的settings.xml文件。

解决办法:删掉maven默认的去找的那个settings.xml文件,这样自定义的文件就会生效了

2. pom.xml的配置

<build>
        <plugins>

              <!-- 添加插件 关联testNg.xml  ,surefire插件其实就是一个容器-->
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>

                <!-- 有疑问 ,加上这段代码就没有多余的report文件了-->
                <!-- 添加插件,添加ReportNg的监听器,生成TestNg的报告 -->
                     <properties>
                        <property>
                            <name>usedefaultlisteners</name>
                            <value>false</value>
                        </property>                        
                        <property>
                            <name>listener</name>
                            <value>org.uncommons.reportng.HTMLReporter</value>
                        </property>
                    </properties>

                    <suiteXmlFiles>
                        <suiteXmlFile>res/testNG.xml</suiteXmlFile>
                    </suiteXmlFiles>

                    <workingDirectory>target/</workingDirectory>
                </configuration>
            </plugin>  

 <!--

  这里要将defaultListener设置为false,下面配置了两个listener,一个是HTMLReport,用来生成HTML格式的Report,
 另一个是JUnitXMLReporter,这个是用来生成xml格式的report,用于jekins服务器;practice11.xml即为即将运行的TestNG xml。
然后运行maven test,生成的报告会在target/目录下。
有了报告以后,我们会想,能不能在case执行的过程中将一些重要的log信息也输出到report中呢?
testNG中有这样一个类:Reporter,就是专门做这个工作的。
@Test
    public void test1(){
        Reporter.log("This is test1");
    }
这样生成的报告就既美观又实用了。
除了ReportNG外,我们还可以使用testNG-xslt

 -->


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.8.0_131\bin\javac</executable>
                </configuration>
            </plugin>  

        </plugins>
    </build>

3.记得在eclipse上装好TestNG插件

4.在装好Jenkins的时候,在真正开始做构建配置前,务必装好git客户端,然后把git.exe的路径放到 Jenkins的 主页面 > 系统管理 > Global Tool Configuration中。

那个Git那一项下面的Path to Git executable的路径一定要一开始就设置好【这个上面应该填写,例如:C:\Program Files\Git\bin\git.exe】(如果你是通过github.com上面的git项目和Jenkins进行关联,就要设置Path to Git executable这一项)

并在自己建立的Jenkins项目下,点击左边列表的“配置”->“General”选项卡->源码管理->在认证上面,点击Add,输入自己Github账号和密码,然后添加进来,最后输入https://github.com/github的ID/项目名xxx.git  这样的URI。

参考资料:

http://www.cnblogs.com/mingmingruyuedlut/p/4228576.html

http://blog.csdn.net/wangfei0904306/article/details/56011877

http://www.cnblogs.com/zhengah/p/5168142.html

5. 启动和停止Jenkins服务

启动Jenkins服务

  net start jenkins  (注:如果Jenkins曾经启动过,启动服务不需要进入到某个目录)

停止Jenkins服务

  net stop jenkins