在同一个 Maven 项目中创建和使用 Web 服务
我正在尝试构建一个 Maven 项目,一个包含 Web 服务的 OSGi 包。我使用 JAX-WS 和所有 @WebService
注释来指定我拥有的 Web 服务。要在客户端位置加载这些 Web 服务,您通常使用 wsgen
和 wsimport
来导出/导入 WSDL 文件。我计划使用 jaxws-maven-plugin 来执行此操作,但问题是:
该包可以同时充当服务器和客户端。它可以将自己注册为同一包的父节点(在不同的 JVM/主机上运行)的客户端。所以这个maven项目/bundle为webservice定义了一个接口,并定义了一个实现这个接口的实现类。接口和类都像往常一样使用 @WebService
注释。
@WebService
public interface Example {
public void callMe();
}
@WebService
public class ExampleImpl implements Example {
public void callMe() {};
}
然后在我的代码中的某个地方:
Endpoint p = Endpoint.publish(
"http://localhost:8080/example",
new ExampleImpl());
jaxws:wsgen goal< /a> 读取注释并创建输出文件(.class 文件、.java 文件、WSDL 文件,具体取决于配置...)。但是如何在 jaxws:wsimport< 期间使用这些文件/a> 相同 mvn package
运行的目标?在同一个 Maven 项目中,我想使用这个 Web 服务,所以我需要编写如下内容:
ExampleImplService service = new ExampleImplService();
Example port = service.getExampleImplPort();
port.callMe();
jaxws:gen
目标在 process-classes
阶段运行,因为它需要读取已编译的类,但必须在 generate-sources
阶段运行 jaxws:import
来准备编译的所有内容。现在我遇到了先有鸡还是先有蛋的问题。我需要编译的类通过 wsgen
生成输出文件,但我需要 wsgen
的输出文件用于 generate 中的
wsimport
maven 的-sources 阶段。我的第一次尝试是将 jaxws:wsgen 目标也分配给生成源阶段,但当然它不起作用,因为类丢失/尚未编译。
我有什么选择来解决这个问题?我是否应该在 generate-sources 之前运行
阶段,以便 jaxws:wsgen 可以使用它(在该阶段),创建输出文件,然后由 antrun
目标来编译一些类(即仅具有 @WebService
注释的类)generate 中的 jaxws:wsimport
使用-来源阶段?还有其他方法可以解决先有鸡还是先有蛋的问题吗?是否还有其他“maven 方法”用于在同一个 Maven 项目中编译 Web 服务的服务器和客户端部分?顺便说一句,应该是这样。从干净的 mvn clean
构建运行,所以我不想要/喜欢任何解决方案,例如“运行 mvn package
两次以首先生成 webservices 文件,然后编译其他所有内容”。换句话说:mvn clean package
应该编译整个maven项目/osgi包。
I'm trying to build a maven project, an OSGi bundle, which contains Webservices. I'm using JAX-WS with all the @WebService
annotations to specify the webservices I have. To load these Webservices at a client location you are normally using wsgen
and wsimport
for exporting/importing WSDL files. I plan to use the jaxws-maven-plugin to do so, but here is the problem:
The bundle can act as a server and client at the same time. It can register itself as a client to a parent node of the same bundle (running on a different JVM/host). So this maven project/bundle defines an interface for the webservice and define an implementation class which implements this interface. Both interface and class use the @WebService
annotation as usual.
@WebService
public interface Example {
public void callMe();
}
@WebService
public class ExampleImpl implements Example {
public void callMe() {};
}
And then somewhere in my code:
Endpoint p = Endpoint.publish(
"http://localhost:8080/example",
new ExampleImpl());
The jaxws:wsgen goal reads the annotations and create the output files (.class files, .java files, WSDL files, depending on the configuration...). But how do I use these files during the jaxws:wsimport goal for the same mvn package
run? In the same maven project I want to use this webservice, so I need to write something like this:
ExampleImplService service = new ExampleImplService();
Example port = service.getExampleImplPort();
port.callMe();
The jaxws:gen
goal is running in the process-classes
phase as it needs to read the compiled classes, but jaxws:import
must be run in the generate-sources
phase to prepare everything for compiling. And now I run in a chicken-egg problem. I need the compiled classes to generate the output files via wsgen
, but I need the output files of wsgen
for wsimport
in the generate-sources
phase of maven. My first try was to assign the jaxws:wsgen
goal to the generate-sources
phase as well but of course its not working as the classes are missing/not compiled yet.
What are my options to solve this problem? Should I run an antrun
goal to compile some classes (namely only the classes with the @WebService
annotations) prior the generate-sources
phase so jaxws:wsgen
can use it (in that phase), create the output files which are then used by jaxws:wsimport
in the generate-sources
phase? Are there other ways to solve this chicken-egg problem? Are there any other "maven ways" for compiling the server and client part of webservices in the same maven project? It should btw. run from a clean mvn clean
build, so I don't want/like any solutions like "run mvn package
twice to generate the webservices files first and then to compile everything else". In other words: mvn clean package
should compile the whole maven project/osgi bundle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将 jaxsw:wsgen 目标移至generate-sources 阶段成功解决了这个问题。我使用以下步骤。
antrun
执行编译带有@WebService
注释的类,该执行使用
来编译类。我将生成的 .class 文件保存在临时目录中,该目录在创建客户端存根后被删除。jaxws:wsgen
目标从已编译的 .class 文件创建 WSDL 文件。jaxws:wsimport
目标的客户端存根。生成的 pom.xml 文件如下所示(仅相关部分)
I have managed to solve this problem by moving the
jaxsw:wsgen
goal to thegenerate-sources
phase. I use the following steps.@WebService
annotations via anantrun
execution, which use<javac>
to compile the classes. I save the resulting .class files in a temporary directory which is deleted after I have created the client stubs.jaxws:wsgen
goal.jaxws:wsimport
goal.antrun
execution.The resulting pom.xml file looks as follow (only the relevant parts)