gpt4 book ai didi

java - 如何将 NetBeans Java Web 应用程序项目的 jar 文件添加到 Ant,以便在 Jenkins 上构建它?

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:33 28 4
gpt4 key购买 nike

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

我的 Ant 目标是干净调试。 Clean 可以工作,但在尝试编译项目时调试失败。

在 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.CopyLib s.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 服务库。我在 C:/Program Files/wildfly-8.1.0.Final/modules/system/layers/base/javax/ws/rs/api/main/jaxrs-api-3.0.8.Final.jar 找到了我需要的 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 文件中声明的任何内容发生冲突。有人也知道这件事吗?

最佳答案

好吧,我的同事发现我们只需将 NetBeans 项目的私有(private)属性复制到我们项目的 Jenkins 作业的 Ant Invoke 的 Properties 字段即可。

因此,我们的 NetBeans 项目在 nbproject 目录中有一个名为 private 的目录。在这个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 Services的jar,我们只需要正确设置WildFly服务器的属性即可。

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

关于java - 如何将 NetBeans Java Web 应用程序项目的 jar 文件添加到 Ant,以便在 Jenkins 上构建它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25162352/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com