gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:48:01 27 4
gpt4 key购买 nike

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

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

最佳答案

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

如果您使用 Eclipse,请遵循以下步骤。

  1. 选择新项目->maven项目->并在新maven项目中选择原型(prototype)

     groupId:org.apache.maven.achetypes
    artifactId:maven-achetype-plugin
  2. 添加 commons-io dependentecny 并将版本更改为 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安装

  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>

注意

  1. 我已经给出了替换文本的基本示例。
  2. 为了方便起见,我已对 groupId 和 arifactId 进行了相应更改。
  3. 最后,插件的顺序应该保持 1->cxf 2. pom.xml 中的自定义插件,它仅适用于 maven 3.0.2 或更高版本。

关于java - CXF:如何使用wsdl2java生成@Logging注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com