在同一个 Maven 项目中创建和使用 Web 服务

发布于 2024-10-17 02:42:19 字数 2205 浏览 2 评论 0原文

我正在尝试构建一个 Maven 项目,一个包含 Web 服务的 OSGi 包。我使用 JAX-WS 和所有 @WebService 注释来指定我拥有的 Web 服务。要在客户端位置加载这些 Web 服务,您通常使用 wsgenwsimport 来导出/导入 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 之前运行 antrun 目标来编译一些类(即仅具有 @WebService 注释的类) 阶段,以便 jaxws:wsgen 可以使用它(在该阶段),创建输出文件,然后由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-10-24 02:42:19

我通过将 jaxsw:wsgen 目标移至generate-sources 阶段成功解决了这个问题。我使用以下步骤。

  1. 首先,我通过 antrun 执行编译带有 @WebService 注释的类,该执行使用 来编译类。我将生成的 .class 文件保存在临时目录中,该目录在创建客户端存根后被删除。
  2. 我根据 jaxws:wsgen 目标从已编译的 .class 文件创建 WSDL 文件。
  3. 我从临时目录中创建具有正常 jaxws:wsimport 目标的客户端存根。
  4. 我通过第二次 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>

I have managed to solve this problem by moving the jaxsw:wsgen goal to the generate-sources phase. I use the following steps.

  1. First I compile the classes with @WebService annotations via an antrun 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.
  2. I create the WSDL file from the compiled .class files with the jaxws:wsgen goal.
  3. From the temporary directory I create the client stubs with the normal jaxws:wsimport goal.
  4. I delete the temporary directory with a second antrun execution.

The resulting pom.xml file looks as follow (only the relevant parts)

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文