天天看点

maven实现webservice的流程

在用maven进行开发之前,首先需要在eclipse里面安装maven插件;

m2eclipse Plugin: http://m2eclipse.sonatype.org/sites/m2e

webservice的一个hello的实例

1.新建一个Maven project

2.直接下一步

3.选者--quictstart

4.group ID(包名)=cn.org.soa.tour.goshawk ------ Artifact ID(大工程名称) version(版本)默认

5.完成

6.删除src目录(源码包目录)

7.点击右键 - 看见build patch

8.点击source 删除所有 --然后 OK

9.然后项目会出现“叹号“ 然后改变JRE 版本为1。6

10.进入pom.xml, 选择dependecles, 删除junit 然后保存

11.修改packaging 为"pom" 保存

12.在该项目下,右键,选择"Maven Module",然后next

13.输入一个此项目名称

14.选者Group ID="org.apache.Maven.archetypes" Archefact ID=maven-archetype-webapp version=1.0 然后next

15.修改package 其他为默认 最后 finish

16.修改刚才所建立的项目的 JRE 版本为1。6

17.打开该项目下的pom.xml,删除"junit"

18.打开总项目下的 pom.xml, 选者plugin,接着选择添加,输入 “maven-compiler”,点击OK,保存

19.接着,打开总项目下的pom.xml,添加<plugin>标签

<configuration>

<source>1.6</source>

<target>1.6</target>

</configuration>

在<build>下的<plugins>下的<plugin>

20.打开子项目下的pom.xml,选择depencies,点击add,输入“jaxws-rt”,选者"com.sun.xml.ws",接着选择"2.1.4"版本,"OK"保存

21.选者plugin,点击 add,输入“jaxws”,打开,选择 "org.codehaus.mojo",version=1.10

22.选者plugin,找到Executions,点击create,找到Execution Detail,点击create.这步完成后,在Goals下应该出现一个“?”,点击此问号,修改为“wsgen”

23.在此项目下的,源码包src/main/下建一个文件夹,命名为java.选择新建的文件夹java,右键build patch,选择 use as folder

24.在新建文件java下,新建一个包,并且新建一个JAVA类,在类中写入

@WebService

public class HelloWord {

@WebMethod

public String sayHello(String name){

return "hello " + name;

}

这个可能会出现JRE版本问题,如果出现此问题,点击项目右键,选者perperty,再选者java compilter,选者版本为1.6,勾选上

25.打开子项目下的pom.xm,在<url></url> 下添加:

<repositories>

<repository>

<id>maven-repository.dev.java.net</id>

<name>Java.net Repository for Maven 1</name>

<url>http://download.java.net/maven/1/</url>

</repository>

<repository>

<id>maven2-repository.dev.java.net</id>

<name>Java.net Repository for Maven 2</name>

<url>http://download.java.net/maven/2/</url>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>maven2-repository.dev.java.net</id>

<url>http://download.java.net/maven/2/</url>

</pluginRepository>

</pluginRepositories>

26.打开子目录下的pom.xml,在<plugins>下添加:

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>jaxws-maven-plugin</artifactId>

<version>1.10</version>

<configuration>

<sei>cn.org.soa.tour.goshawk.HelloWord</sei>

<keep>true</keep>

</configuration>

<executions>

<execution>

<goals>

<goal>wsgen</goal>

</goals>

</execution>

</executions>

</plugin>

其中:<sei>为自己刚刚新建的类的全名

27.修改该项目下的 web.xml,改变为:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

注意: 这是一个追加步骤------在本机的登录用户下建立一个.m2文件夹,在文件夹里面建立一个 settings.xml文件,在文件中写入如下的内容

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0

http://maven.apache.org/xsd/settings-1.0.0.xsd">

<localRepository/>

<interactiveMode/>

<usePluginRegistry/>

<offline/>

<pluginGroups/>

<servers/>

<mirrors/>

<proxies/>

<profiles/>

<activeProfiles/>

</settings>

28.在子项目下,右键,选择run as,选择Maven pakage

29.下面进入测试步骤:

一:启动WebLogic

二:在浏览器中输入:http://localhost:7001/console

三:进入后,点击deploy(部署),选择上传,上传为刚刚打包的war文件,接着发布这个项目。最后选择测试,在测试中选择“测试客户机”。

30.在总项目下再建立一个Maven Modul项目 ‘client’,然后下一步,选择默认.下一步,修改package.

31.修改此项目下的pom.xml,选择plugin,点击add,输入jaxws-,打开包,选择版本为1.10

32.还在此XML文件下,找到Executions,点击create,找到Execution Detail,点击create.这步完成后,在Goals下应该出现一个“?”,点击此问号,修改为“wsimport”

32.还在此XML下,在<version></version>下添加:

<configuration>

<wsdlUrls>

<wsdlUrl>http://localhost:7001/hellowordserivce/HelloWordService?WSDL</wsdlUrl>

</wsdlUrls>

<packageName>cilent.hellword</packageName>

</configuration>

注意:<wsdlUrl>为你的项目发布路径(可以在开始你发布的项目下看到),<packageName>为你的JAVA文件所在的包路径

33.右击客户端项目,选择run as 选择Maven source:jar

34.点击客户端项目中的targge,第一次可能其中没有内容,可以刷新此目录

35.进入此目录(target),一直进入到JAVA目录,点击右键,选择build patch,选择 use as source folder

36.在源文件夹下建立一个JAVA普通文件:

内容事例:public class HelloWorldClientTest {

public static void main(String[] args) {

HelloWordService hws = new HelloWordService();//service项目中的JAVA类

HelloWord helloworld= hws.getHelloWordPort();//HelloWord 为Client项目的JAVA类

System.out.println(helloworld.sayHello("Game Over"));

}

}

以上就是利用maven实现一个简单webservicer的全部流程

maven远程仓库setting.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<profiles>

<profile>

<repositories>

<repository>

<snapshots>

<enabled>false</enabled>

</snapshots>

<id>central</id>

<name>libs-releases</name>

<url>http://maven.soatour.org:8081/artifactory/libs-releases</url>

</repository>

<repository>

<snapshots />

<id>snapshots</id>

<name>libs-snapshots</name>

<url>http://maven.soatour.org:8081/artifactory/libs-snapshots</url>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<snapshots>

<enabled>false</enabled>

</snapshots>

<id>central</id>

<name>plugins-releases</name>

<url>http://maven.soatour.org:8081/artifactory/plugins-releases</url>

</pluginRepository>

<pluginRepository>

<snapshots />

<id>snapshots</id>

<name>plugins-snapshots</name>

<url>http://maven.soatour.org:8081/artifactory/plugins-snapshots</url>

</pluginRepository>

</pluginRepositories>

<id>artifactory</id>

</profile>

</profiles>

<activeProfiles>

<activeProfile>artifactory</activeProfile>

</activeProfiles>

</settings>