我正在尝试构建一个 Maven 项目,一个包含 Web 服务的 OSGi 包。我使用带有所有 @WebService
注释的 JAX-WS 来指定我拥有的 Web 服务。要在客户端位置加载这些 Web 服务,您通常使用 wsgen
和 wsimport
来导出/导入 WSDL 文件。我打算使用 jaxws-maven-plugin这样做,但问题是:
bundle 可以同时充当服务器和客户端。它可以将自己注册为同一包的父节点的客户端(在不同的 JVM/主机上运行)。所以这个 Maven 项目/包为 web 服务定义了一个接口(interface),并定义了一个实现这个接口(interface)的实现类。接口(interface)和类都像往常一样使用 @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读取注释并创建输出文件(.class 文件、.java 文件、WSDL 文件,具体取决于配置...)。但是我如何在 jaxws:wsimport 期间使用这些文件?同一 mvn package
运行的目标?在同一个maven项目中我想使用这个webservice,所以我需要写这样的东西:
ExampleImplService service = new ExampleImplService();
Example port = service.getExampleImplPort();
port.callMe();
jaxws:gen
目标在 process-classes
阶段运行,因为它需要读取编译后的类,但是 jaxws:import
必须在 generate-sources
阶段运行,为编译做好一切准备。现在我遇到了先有鸡还是先有蛋的问题。我需要编译类通过 wsgen
生成输出文件,但我需要 wsgen
的输出文件用于 wsimport
在 generate -sources
Maven 的阶段。我的第一个尝试是将 jaxws:wsgen
目标也分配给 generate-sources
阶段,但当然它不起作用,因为类丢失/尚未编译。
解决这个问题我有哪些选择?我是否应该在 generate-sources 之前运行
阶段以便 antrun
目标来编译 some 类(即只有带有 @WebService
注释的类) jaxws:wsgen
可以使用它(在该阶段),创建输出文件,然后由 jaxws:wsimport
在 generate -sources
阶段?还有其他方法可以解决这个先有鸡还是先有蛋的问题吗?在同一个 Maven 项目中是否有其他“Maven 方式”来编译 Web 服务的服务器和客户端部分?它应该顺便说一句。从干净的 mvn clean
构建运行,所以我不想/喜欢任何解决方案,例如“运行 mvn package
两次以首先生成 web 服务文件,然后编译其他所有内容”。换句话说:mvn clean package
应该编译整个 maven 项目/osgi 包。
最佳答案
我设法通过移动 jaxsw:wsgen
解决了这个问题。目标 generate-sources
阶段。我使用以下步骤。
- 首先我用
@WebService
编译类通过antrun
注释执行,使用<javac>
编译类。我将生成的 .class 文件保存在创建客户端 stub 后删除的临时目录中。 - 我使用
jaxws:wsgen
从编译的 .class 文件创建 WSDL 文件目标。 - 在临时目录中,我使用正常的
jaxws:wsimport
创建客户端 stub 目标。 - 我用第二个
antrun
删除了临时目录执行。
生成的 pom.xml 文件如下所示(仅相关部分)
<properties>
<tmpdirectory>${java.io.tmpdir}${file.separator}${user.name}-${project.groupId}-${project.artifactId}</tmpdirectory>
</properties>
...
<plugin>
<!-- clean tmp directory at every "mvn clean" -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>${tmpdirectory}</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<!-- compile webservice classes into tmp directory -->
<id>mini compile of webservices</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<mkdir dir="${tmpdirectory}" />
<javac includeAntRuntime="false"
classpath="${compile_classpath}"
destdir="${tmpdirectory}">
<src path="${project.build.sourceDirectory}" />
<include name="org/example/project/*/webservice/*.java" />
</javac>
</target>
</configuration>
</execution>
<execution>
<!-- delete temporary directory (in case mvn clean is not called) -->
<id>clean up tmp dir</id>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete dir="${tmpdirectory}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<!-- generate WSDL file from the compiled classes in tmp directory -->
<id>generate wsdl file</id>
<phase>generate-sources</phase>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
<sei><!-- service endpoint implementation --></sei>
<destDir>${tmpdirectory}</destDir>
<genWsdl>true</genWsdl>
<resourceDestDir>${tmpdirectory}</resourceDestDir>
</configuration>
</execution>
<execution>
<!-- create client stub files -->
<id>create client files from wsdl file</id>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<keep>true</keep>
<wsdlDirectory>${tmpdirectory}</wsdlDirectory>
</configuration>
</execution>
</executions>
</plugin>
关于java - 在同一个 Maven 项目中创建和使用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967472/