maven可以告诉eclipse忽略生成的代码中的警告吗?

时间:2018-02-10 20:53:51

标签: java eclipse maven code-generation

我有一些Java项目。它是一系列API。我使用swagger codegen从swagger定义生成大量服务器代码。我正在使用带有委托模式的spring-boot,因此我生成的代码全部转到src/gen/java/main,我可以在src/main/java中编写实现代码。生成的代码不受版本控制,但可以根据需要由maven swagger codegen插件重新生成。所有这一切都很好用:)

但是,当我第一次将项目导入Eclipse(在父项目中使用“import existing maven project”将它们全部导入)时,我从生成的代码中获得了一堆“未使用的函数”类型警告。 (我使用 build-helper-maven-plugin src/gen/java/main文件夹添加为源文件夹。)如果我在每个项目中选择src/gen/java/main文件夹,请单击鼠标右键,选择属性并说忽略可选的编译问题然后这就消失了(我也将它标记为派生资源)

问题:有没有办法在pom中标记此文件夹,以便当我(或同事)将项目导入Eclipse时,这些设置已经设置在该文件夹中?或者,某种方式告诉eclipse总是以这种方式处理名称(相对于项目路线)的文件夹?

其他信息

我在评论中被要求提供pom文件。我做了一个相当小的例子:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.api</groupId>
    <artifactId>com.example.api</artifactId>
    <packaging>jar</packaging>
    <name>Example</name>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <springfox-version>2.7.0</springfox-version>
        <swagger.codegen.version>2.4.0-SNAPSHOT</swagger.codegen.version> 
        <jetty-version>9.2.15.v20160210</jetty-version>
        <slf4j-version>1.7.21</slf4j-version>
        <junit-version>4.12</junit-version>
        <servlet-api-version>2.5</servlet-api-version>
        <springfox-version>2.7.0</springfox-version>
        <jackson-version>2.8.9</jackson-version>
        <jackson-threetenbp-version>2.6.4</jackson-threetenbp-version>
        <spring-version>4.3.9.RELEASE</spring-version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <pluginManagement>
            <plugins>                   
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>io.swagger</groupId>
                                        <artifactId>swagger-codegen-maven-plugin</artifactId>
                                        <versionRange>${swagger.codegen.version}</versionRange>
                                        <goals>
                                            <goal>generate</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>              
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.example.api.Swagger2SpringBoot</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Needed to create swagger bits in asynch manner -->
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>${swagger.codegen.version}</version>
                <executions>
                    <execution>
                        <id>foo</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/spec/foo.yaml</inputSpec>                     
                            <modelPackage>com.example.api.models</modelPackage>
                            <apiPackage>com.example.api</apiPackage>
                            <language>spring</language>
                            <invokerPackage>com.example.api</invokerPackage>
                            <basePackage>com.example.api</basePackage>
                            <withXml>true</withXml>
                            <configOptions>
                                <artifactId>bookings</artifactId>
                                <artifactDescription>Bookings API</artifactDescription>
                                <title>Bookings API</title>
                                <artifactUrl>https://api.example.com/foo</artifactUrl>
                                <groupId>com.example.api</groupId>
                                <artifactVersion>1.0</artifactVersion>
                                <configPackage>com.example.api.config</configPackage>
                                <serializableModel>true</serializableModel>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                                <async>true</async>
                                <library>spring-boot</library>      
                                <delegatePattern>true</delegatePattern>
                                <useBeanValidation>true</useBeanValidation>
                                <useOptional>true</useOptional> 
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>                                                             
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-version}</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/v2</contextPath>
                    </webAppConfig>
                    <webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
                <stopPort>8079</stopPort>
                <stopKey>stopit</stopKey>
                <httpConnector>
                    <port>8002</port>
                    <idleTimeout>60000</idleTimeout>
                </httpConnector>
            </configuration>

            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!--SpringFox dependencies -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    <!-- Bean Validation API support -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <scope>provided</scope>
    </dependency>    
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>
</dependencies>
</project>

这使用了最小的foo.yaml:

swagger: '2.0'
info: 
  title: Foo API
  description: Test case
  version: 1.0
  host: api.example.com
basePath: /
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json

tags:
  - name: foo
parameters:
  message:
    name: message
    in: body
    description: Foo
    schema:
      $ref: '#/definitions/Message'
    required: true
definitions:
  Message:
    type: object
    description: Foo
    properties:
      heading:
        type: string
        description: heading
      body:
        type: string
        description: body
paths:
  /foo:
    post:
      summary: foo
      operationId: postFoo
      tags:
        - foo
      parameters:
        - $ref: '#/parameters/message'
      responses:
         '202':
            description: Messages will be sent
         default:
          description: An unexpected error occurred

如果我只是mvn clean compile然后导入它就可以了。但是,如果我添加使用生成的代码的任何实现代码,那么它不是。

例如,我向com.example.api.implementation添加了一个包src/main/java,其中包含 FooApi.java 文件:

package com.example.api.implementation;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import com.example.api.FooApiDelegate;
import com.example.api.models.Message;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class FooApi implements FooApiDelegate {
    private final ObjectMapper objectMapper;

    private final HttpServletRequest request;

    public Optional<ObjectMapper> getObjectMapper() {
        return Optional.ofNullable(objectMapper);
    }

    public Optional<HttpServletRequest> getRequest() {
        return Optional.ofNullable(request);
    }

    @org.springframework.beans.factory.annotation.Autowired
    public FooApi(ObjectMapper objectMapper, HttpServletRequest request) {
        this.objectMapper = objectMapper;
        this.request = request;
    }

    @Override
    public CompletableFuture<ResponseEntity<Void>> postFoo( Message  message) {
        return new CompletableFuture<ResponseEntity<Void>>();
    }
}

如果我现在导入,我得到错误 FooApiDelegate无法解析为类型消息无法解析为类型(以及相应的导入)来自我的非.generated file。

1 个答案:

答案 0 :(得分:0)

至少有两个问题。第一件事是您使用org.eclipse.m2e明确地抑制了代码生成的执行。这将抑制您可能拥有的任何类型的生成。此外,您使用的是2.4.0-SNAPSHOT,您应该使用3.0.0-rc0 instead。不幸的是3.0.0-rc0失败了所以你应该坚持使用2.3.1而不是SNAPSHOT&#39>。

除了缺少插件外,还没有正确处理事情......如果你在Eclipse中干净地导入项目,你将得到一个关于Setup Maven Plugin Connectors的对话框。此外,插件无法正确处理Eclipse上下文中的更新...

如果您导入项目并从target/generated-sources手动添加源文件夹,这将有效,但遗憾的是并非总是......

相关问题