在项目中使用了cxf的wsdl2java插件,自动根据wsdl生成java代码。(具体方法见前文:http://blog.csdn.net/mypop/archive/2011/01/17/6147356.aspx)但是由于公司需要使用代理上网,每次使用该插件的时候,均需要手动设置jvm代理(我的方法事修改$JAVA_HOME/jre/lib/net.properties文件中的http.proxyHost等属性),非常麻烦,因此打算开发一个设置jvm代理的plugin,在执行wsdl2java前先将环境设置好。
下面是开发步骤:
1. 创建项目
mvn archetype:create -DgroupId=com.my.plugins -DartifactId=maven-httpproxy-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo
2. 创建Mojo
package com.my.plugins; /* * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import java.util.Properties; /** * Goal which touches a timestamp file. * * @goal setproxy * */ public class HttpProxyMojo extends AbstractMojo { /** * HTTP Proxy Server * @parameter expression="${httpproxy.server}" * @required */ private String server; /** * HTTP Proxy Port * @parameter expression="${httpproxy.port}" * @required */ private String port; /** * HTTP Nonproxy Host * @parameter expression="${httpproxy.port}" default-value="localhost" */ private String exception; public void execute() throws MojoExecutionException { getLog().info("Set JVM HTTP Proxy"); Properties prop = System.getProperties(); // 设置http访问要使用的代理服务器的地址 prop.setProperty("http.proxyHost", server); // 设置http访问要使用的代理服务器的端口 prop.setProperty("http.proxyPort", port); // 设置不用代理访问的host prop.setProperty("http.nonProxyHosts", exception); } }
maven 插件使用注解配置goal、参数等,具体参阅《maven权威指南》关于插件的部分
3. 测试
mvn com.my.plugins:maven-httpproxy-plugin:1.0-SNAPSHOT:setproxy -Dhttpproxy.server=test.com -Dhttpproxy.port=testport
build successful 且有log输出,则插件正确
4. install
mvn install
5. 配置在需要的pom中
<build> <plugins> <plugin> <groupId>com.travelsky.ota</groupId> <artifactId>maven-httpproxy-plugin</artifactId> <version>1.0</version> <executions> <execution> <id>set-jvm-httpproxy</id> <phase>generate-sources</phase> <configuration> <server>test.com</server> <port>8080</port> </configuration> <goals> <goal>setproxy</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
以上就是整个的开发配置过程。
命令如下:
mvn deploy:deploy-file -DgroupId=com.my.plugins -DartifactId=maven-httpproxy-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=maven-httpproxy-plugin-1.0.jar -Durl=http://host:8081/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty
注意,此时插件已经改为正式版,而非snapshot,否则在发布至thirdparty库时,会出现return code is:400这样的错误。