CXF:如何使用wsdl2java生成@Logging注释?

时间:2014-08-05 00:52:58

标签: java maven logging cxf wsdl2java

我们将CXF用于SOAP客户端并使用cxf-codegen-plugin(wsdl2java)maven插件生成客户端(我认为这只是围绕wsd2java的包装器)。我们通常习惯在每次生成后为每个服务添加@Logging(pretty = true)注释。

有没有办法以某种方式自动生成此注释?

1 个答案:

答案 0 :(得分:2)

不知道是否有任何插件可以更改Log语句,但我想最简单的方法是创建将在cxf- code gen插件之后运行的插件。

如果您正在使用eclipse,请遵循以下步骤。

  1. 选择新项目 - > maven项目 - >并在新的maven项目中选择原型

     groupId:org.apache.maven.achetypes
     artifactId:maven-achetype-plugin
    
  2. 添加commons-io dependecny并将版本更改为1.0.0.0

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    
  3. 编辑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);
    }
    

    }

  4. 运行maven install

  5. 现在将插件添加到您的项目

    <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>
    
  6. 注意

    1. 我已经提供了基本样本来替换文本。
    2. 我已经给了groupId和atrifactId,我相应地改变了它。
    3. 最后,插件的顺序应保持在pom.xml中的1-&gt; cxf 2.自定义插件中,并且仅适用于maven 3.0.2或更高版本。