2017-02-23 70 views
0

我要實現我的gradle中沒有使用Maven如下:搖籃定製編譯

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.6.1</version> 
     <executions> 
      <!-- 1. Exclude Default Compiler --> 
      <execution> 
       <id>default-compile</id> 
       <phase>none</phase> 
      </execution> 
      <!-- 2. Compile without Mappers. --> 
      <execution> 
       <id>Compile without Mappers</id> 
       <goals> 
        <goal>compile</goal> 
       </goals> 
       <configuration> 
        <excludes> 
         <exclude>**/**/mapper/**/*</exclude> 
        </excludes> 
       </configuration> 
      </execution> 
      <execution> 
       <!-- 3. Compile Mappers --> 
       <id>Compile Mappers</id> 
       <goals> 
        <goal>compile</goal> 
       </goals> 
       <configuration> 
        <includes> 
         <include>**/**/mapper/**/*</include> 
         <include>${project.build.directory}/classes/**/*</include> 
        </includes> 
        <annotationProcessors> 
         <processor>org.mapstruct.ap.MappingProcessor</processor> 
        </annotationProcessors> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 

什麼,我做的是禁用默認的編譯,編譯所有的源文件除了我Mapstruct映射器,然後編譯Mapstruct映射器包括之前編譯的源代碼。我對gradle很陌生,不知道如何實現類似的功能。我到目前爲止試過的是:

task compileWithoutMapperClasses(type: JavaCompile) { 
    options.incremental = true 
    // Compile without Mappers 
    sourceSets.main.java.exclude '**/**/mapper/**/*' 
} 


task compileMapperClasses(type: JavaCompile) { 
    sourceSets.main.java.include '**/**/mapper/**/*' 
    sourceSets.main.java.include 'build/classes/**/*' 

    options.define(
     compilerArgs: [ 
      "-nowarn", 
      "-proc:only", 
      "-encoding", "UTF-8", 
      "-processor", "org.mapstruct.ap.MappingProcessor", 
     ] 
    ); 
} 

compileMapperClasses.dependsOn compileWithoutMapperClasses 
compileJava.dependsOn compileWithoutMapperClasses 

結果實際上沒有任何東西被編譯。我想這樣做的原因是我想在我的項目中使用lombok和mapstruct。

回答

0

嘗試

task compileMapperClasses(type: JavaCompile) { 
    includes '**/**/mapper/**/*' 
    includes 'build/classes/**/*' 

    options.define(
     compilerArgs: [ 
      "-nowarn", 
      "-proc:only", 
      "-encoding", "UTF-8", 
      "-processor", "org.mapstruct.ap.MappingProcessor", 
     ] 
    ); 
} 

欲瞭解更多信息,請JavaCompile任務https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html

+2

如果使用MapStruct 1.2.0.Beta1和龍目島14年1月16日的confiugration,它應該有一個單一的編制工作。我們兩個團隊一起努力確保MapStruct能夠「看到」龍目島所做的AST修正。 – Gunnar

+0

有沒有一個例子如何用gradle實現這個功能?使用提到的版本不能解決我的問題。 – kukudas

+1

[mapstruct-examples]中有一個示例(https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-lombok)存儲庫。這與@Gunnar在他的評論中寫道的版本一起工作 – Filip