[英]CXF: How to generate @Logging annotation with wsdl2java?
We use CXF for SOAP cients and generate client with cxf-codegen-plugin (wsdl2java) maven plugin (which is I think just a wrapper around wsd2java). 我们将CXF用于SOAP技术,并使用cxf-codegen-plugin(wsdl2java)maven插件(这是wsd2java的包装)生成客户端。 We usually used to add @Logging(pretty=true) annotation to every service every time after the generation.
通常,我们通常在生成后每次都向每个服务添加@Logging(pretty = true)注释。
Is there a way to automatically generate this annotation somehow? 有没有办法以某种方式自动生成此注释?
No Idea if there are any plugins to change Log statements, however I guess easiest way is to create plugin which will run after cxf- code gen plugin. 不知道是否有任何插件可以更改Log语句,但是我想最简单的方法是创建将在cxf-code gen插件之后运行的插件。
Here is the steps to follow if you are using eclipse. 如果您使用的是eclipse,请按照以下步骤操作。
Select new project->maven project-> and in new maven project select archetypes 选择新项目-> maven项目->,然后在新的maven项目中选择原型
groupId:org.apache.maven.achetypes artifactId:maven-achetype-plugin
Add commons-io dependecny and change version to 1.0.0.0 添加commons-ioDependecny并将版本更改为1.0.0.0
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
Edit MyMojo.java 编辑MyMojo.java
package com.kp.plugin.logcodegen; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.FileFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @Mojo(name = "code-gen", defaultPhase = LifecyclePhase.GENERATE_SOURCES) public class MyMojo extends AbstractMojo { @Parameter(defaultValue = "${project.build.directory}") private File outputDirectory; @Parameter(defaultValue = "${basedir}") private File baseDir; @Parameter(defaultValue = "src/main/java", property = "sourceDirecory") private String sourceDirectory; @Parameter(defaultValue = "", property = "packageNames") private String packageNames; @Parameter(defaultValue = "", property = "addImport") private String addImport; @Parameter(defaultValue = "", property = "removeImport") private String removeImport; @Parameter(defaultValue = "", property = "defineLogInstance") private String defineLogInstance; @Parameter(defaultValue = "", property = "removeLogInstance") private String removeLogInstance; public void execute() throws MojoExecutionException { System.out.println("-------------------"); System.out.println("Adding logs to java classes"); System.out.println("--------------------"); System.out.println("Input package is:" + packageNames); System.out.println("BaseDir is " + baseDir.getAbsolutePath()); StringBuilder sourceDir = new StringBuilder(baseDir.getAbsolutePath()); sourceDir.append("/"); sourceDir.append(sourceDirectory); if (!packageNames.isEmpty()) { sourceDir.append("/"); for (final String packageName : packageNames.split(",")) { String path = sourceDir.toString() + packageName.replaceAll("\\\\.", "/"); File dest = new File(path); if (dest.isDirectory()) { Iterator<File> it = FileUtils.iterateFiles(dest, FileFileFilter.FILE, TrueFileFilter.INSTANCE); while (it.hasNext()) { try { processFile(it.next()); } catch (IOException e) { e.printStackTrace(); } } } else { System.out.append("Path is not directory " + path); } } } else { System.out.println("No packages to parse"); } } private void processFile(final File file) throws IOException { List<String> contents = FileUtils.readLines(file); ListIterator<String> it = contents.listIterator(); String className = ""; while (it.hasNext()) { String str = it.next(); // Remove import if (str != null && !str.isEmpty() && str.contains(removeImport)) { it.remove(); it.add(addImport); } if (str != null && !str.isEmpty()) { Pattern pat = Pattern.compile("\\\\s*(public|private)\\\\s+class\\\\s+(\\\\w+)"); Matcher matcher = pat.matcher(str); if (matcher.find()) { className = matcher.group(2); } } // change the instance if (str != null && !str.isEmpty() && str.contains(removeLogInstance)) { it.remove(); it.add(defineLogInstance + className + ".class);"); } } FileUtils.writeLines(file, contents, false); }
} }
run maven install 运行Maven安装
Now add the plugin to your porject 现在将插件添加到您的porject
<pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <versionRange>[2.7,)</versionRange> <goals> <goal>wsdl2java</goal> </goals> </pluginExecutionFilter> <action> <execute /> </action> </pluginExecution> <pluginExecution> <pluginExecutionFilter> <groupId>com.kp.plugin</groupId> <artifactId>logcode-gen</artifactId> <versionRange>[1.0.0.0,)</versionRange> <goals> <goal>code-gen</goal> </goals> </pluginExecutionFilter> <action> <execute /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler.version}</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <encoding></encoding> </configuration> </plugin> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${basedir}/src/main/java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/wsdl/kpws.wsdl</wsdl> <extraargs> <extraarg>-impl</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.kp.plugin</groupId> <artifactId>logcode-gen</artifactId> <version>1.0.0.0</version> <executions> <execution> <id>codegen-resouces</id> <phase>generate-sources</phase> <goals> <goal>code-gen</goal> </goals> </execution> </executions> <configuration> <packageNames>com.kp.webservices.services</packageNames> <addImport>import org.slf4j.Logger;</addImport> <removeImport>java.util.logging.Logger</removeImport> <removeLogInstance>private static final Logger LOG</removeLogInstance> <defineLogInstance>private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(</defineLogInstance> </configuration> </plugin> </plugins>
Note 注意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.