如何为NetBeans Java Web App项目添加一个jar文件到Ant,以便它可以在Jenkins上构建?

时间:2014-08-06 13:58:26

标签: java netbeans ant jenkins restful-architecture

正如我在my previous question中指出的那样,我正在尝试使用Jenkins CI为NetBeans多层Web应用程序项目建立一个自动构建系统。

我的Ant目标是 clean debug 。清理工作,但尝试编译项目时调试失败。

在我的Jenkins构建的输出中,我看到以下命令:

[PGB Webapp] $ cmd.exe / C'"" C:\ Program Files(x86)\ Jenkins \ tools \ hudson.tasks.Ant_AntInstallation \ Apache_Ant \ bin \ ant.bat" " -Dlibs.MySQLDriver.classpath = C:/ Program Files(x86)/ MySQL / MySQL Connector J / mysql-connector-java-5.1.31-bin.jar" " -Dlibs.CopyLibs.classpath = C:/ Program Files / NetBeans 8.0 / java / ant / extra / org-netbeans-modules-java-j2seproject-copylibstask.jar" " -Dj2ee.server.home = C:/ Program Files / wildfly-8.1.0.Final"干净的调试&&退出%% ERRORLEVEL %%"'

这是我输出的最后一部分:(对我来说最重要的部分)

library-inclusion-in-manifest:

-do-compile:
    [mkdir] Created dir: C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\build\empty
    [mkdir] Created dir: C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\build\generated-sources\ap-source-output
    [javac] Compiling 2 source files to C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\build\web\WEB-INF\classes
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\config\ApplicationConfig.java:6: error: package javax.ws.rs.core does not exist
    [javac] import javax.ws.rs.core.Application;
    [javac]                        ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\config\ApplicationConfig.java:8: error: cannot find symbol
    [javac] public class ApplicationConfig extends Application {
    [javac]                                        ^
    [javac]   symbol: class Application
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:12: error: package javax.ws.rs does not exist
    [javac] import javax.ws.rs.GET;
    [javac]                   ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:13: error: package javax.ws.rs does not exist
    [javac] import javax.ws.rs.Path;
    [javac]                   ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:14: error: package javax.ws.rs does not exist
    [javac] import javax.ws.rs.PathParam;
    [javac]                   ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:15: error: package javax.ws.rs does not exist
    [javac] import javax.ws.rs.QueryParam;
    [javac]                   ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:16: error: package javax.ws.rs does not exist
    [javac] import javax.ws.rs.Produces;
    [javac]                   ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:17: error: package javax.ws.rs.core does not exist
    [javac] import javax.ws.rs.core.MediaType;
    [javac]                        ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:19: error: cannot find symbol
    [javac] @Path("/employees")
    [javac]  ^
    [javac]   symbol: class Path
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:29: error: cannot find symbol
    [javac]     public List<Employee> find(@QueryParam("name") String name) {
    [javac]                                 ^
    [javac]   symbol:   class QueryParam
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:27: error: cannot find symbol
    [javac]         @GET
    [javac]          ^
    [javac]   symbol:   class GET
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:28: error: cannot find symbol
    [javac]     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    [javac]      ^
    [javac]   symbol:   class Produces
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:38: error: cannot find symbol
    [javac]     public Employee findById(@PathParam("id") String id) {
    [javac]                               ^
    [javac]   symbol:   class PathParam
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:36: error: cannot find symbol
    [javac]     @GET @Path("{id}")
    [javac]      ^
    [javac]   symbol:   class GET
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:36: error: cannot find symbol
    [javac]     @GET @Path("{id}")
    [javac]           ^
    [javac]   symbol:   class Path
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:37: error: cannot find symbol
    [javac]     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    [javac]      ^
    [javac]   symbol:   class Produces
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:44: error: cannot find symbol
    [javac]     public List<Employee> findByManager(@PathParam("id") String managerId) {
    [javac]                                          ^
    [javac]   symbol:   class PathParam
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:42: error: cannot find symbol
    [javac]     @GET @Path("{id}/reports")
    [javac]      ^
    [javac]   symbol:   class GET
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:42: error: cannot find symbol
    [javac]     @GET @Path("{id}/reports")
    [javac]           ^
    [javac]   symbol:   class Path
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\rest\EmployeeResource.java:43: error: cannot find symbol
    [javac]     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    [javac]      ^
    [javac]   symbol:   class Produces
    [javac]   location: class EmployeeResource
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\config\ApplicationConfig.java:17: error: method does not override or implement a method from a supertype
    [javac]     @Override
    [javac]     ^
    [javac] C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\src\java\com\edw\config\ApplicationConfig.java:22: error: method does not override or implement a method from a supertype
    [javac]     @Override
    [javac]     ^
    [javac] 22 errors

BUILD FAILED
C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\nbproject\build-impl.xml:208: The following error occurred while executing this line:
C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\nbproject\build-impl.xml:881: The following error occurred while executing this line:
C:\Program Files (x86)\Jenkins\workspace\PGB Webapp\pgb-war\nbproject\build-impl.xml:297: Compile failed; see the compiler error output for details.

Total time: 1 second
Build step 'Start Ant' marked build as failure
Finished: FAILURE

嗯,重要的是Java编译器无法找到RESTful Web Services库。我在 C:/ Program Files / wildfly-8.1.0.Final / modules / system / layers / base / javax / ws / rs / api / main / jaxrs-api-3.0.8找到了我需要的jar。 Final.jar

我没有使用Ant构建工具的经验,所以我搜索并发现了很多关于它的内容,但我似乎无法弄清楚如何添加这个库以便项目在Jenkins上正确构建。

目前我的build.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project name="pgb" default="default" basedir="." xmlns:ear="http://www.netbeans.org/ns/j2ee-earproject/2">
    <description>Builds, tests, and runs the project pgb.</description>
    <import file="nbproject/build-impl.xml"/>

    <taskdef resource="net/jtools/classloadertask/antlib.xml">
      <classpath>
        <fileset dir="${basedir}/lib" includes="ant-classloader*.jar"/>
      </classpath>
    </taskdef>

    <!--Add jar to the classpath-->
    <classloader loader="system">
      <classpath>
        <fileset dir="${basedir}/lib" includes="jboss-modules.jar"/>
        <fileset dir="${basedir}/lib" includes="jaxrs-api-3.0.8.Final.jar"/>
      </classpath>
    </classloader>
</project>

文件build-impl.xml(已导入)由NetBeans生成(您可能已经知道)并且太大而无法在此处显示,此外我无法更改任何内容,因为系统可能会自动更改该文件本身。

无论如何,正如您所看到的,我已尝试使用我在此处找到的Ant类加载器:https://stackoverflow.com/a/5116470/2541501 但那里没有运气..我甚至将所需的jar文件添加到项目的lib目录中。

所以,我的问题是:如何更改我的Ant版本,以便Java编译器不会抱怨缺少包(即丢失jar文件)?

我还担心如果我在build.xml文件中添加任何目标,它可能会与生成的build-impl.xml文件中声明的任何内容冲突。任何人都对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

好的,我的同事发现我们只需要将NetBeans项目的私有属性复制到我们项目的Jenkins作业的Ant Invoke 的Properties字段中。

因此,我们的NetBeans项目在 nbproject 目录中有一个名为 private 的目录。在此私有目录中,有一个名为 private.properties 的文件。我们从此文件中获取属性并将其复制到Ant Invoke的Properties字段中。现在我们的属性字段如下所示:

libs.CopyLibs.classpath=C:/Program Files/NetBeans 8.0/java/ant/extra/org-netbeans-modules-java-j2seproject-copylibstask.jar
libs.MySQLDriver.classpath=C:/Program Files (x86)/MySQL/MySQL Connector J/mysql-connector-java-5.1.31-bin.jar

j2ee.platform.is.jsr109=true
j2ee.server.domain=C:/Wildfly/standalone
j2ee.server.home=C:/Wildfly
j2ee.server.instance=wildfly-deployer:localhost:8080#standalone&C:\\Wildfly

现在我们可以毫无问题地执行所有Ant命令。我们还可以部署通过Jenkins构建的Web应用程序。

如果有人想知道,可以在WildFly中找到RESTful Web服务。因此,我们不需要直接包含RESTful Web服务的jar,我们只需要正确设置WildFly服务器的属性。

回想起来,我想说,为NetBeans项目建立一个自动构建的持续集成系统并不难。现在,下一步还包括项目的自动化测试。