2016-02-08 49 views
0

我是新來的gradle,並試圖將我的POM轉換爲gradle.build。我已經轉換了依賴部分,現在轉換插件部分。我在我的POM轉換maven jar和複製資源插件gradle

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>${maven-dependency-plugin.version}</version> 
    <executions> 
     <execution> 
      <id>copy-dependencies</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
       <includeGroupIds> 
        junit, org.apache.logging.log4j, org.springframework, aopalliance, org.springframework.data, javax.inject, 
         org.hibernate, javax.el, com.microsoft.sqlserver, org.apache.commons, com.jcraft, com.sun.mail, 
         org.apache.velocity, commons-lang, commons-logging, commons-collections, org.jboss.logging, 
         org.jboss, org.javassist, dom4j, javax.transaction 
       </includeGroupIds> 
       <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory> 
       <overWriteReleases>false</overWriteReleases> 
       <overWriteSnapshots>false</overWriteSnapshots> 
       <overWriteIfNewer>true</overWriteIfNewer> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>${maven-jar-plugin.version}</version> 
    <configuration> 
     <excludes> 
      <exclude>**/*.properties</exclude> 
      <exclude>**/*.xml</exclude> 
      <exclude>**/*.vm</exclude> 
      <exclude>**/*.txt</exclude> 
     </excludes> 
     <archive> 
      <manifest> 
       <addClasspath>true</addClasspath> 
       <classpathPrefix>dependency-jars/</classpathPrefix> 
      <mainClass>com.softech.ls360.integration.BatchImport</mainClass> 
       </manifest> 
      <manifestEntries> 
       <Class-Path>conf/</Class-Path> 
      </manifestEntries> 
     </archive> 
    </configuration> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.6</version> 
    <executions> 
     <execution> 
      <id>copy-resources</id> 
      <phase>install</phase> 
      <goals> 
       <goal>copy-resources</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${basedir}/target/conf</outputDirectory> 
       <resources> 
        <resource> 
         <directory>src/main/resources</directory> 
         <includes> 
          <include>**/*.properties</include> 
          <include>**/*.xml</include> 
          <include>**/*.vm</include> 
          <include>**/*.txt</include> 
         </includes> 
        </resource> 
       </resources> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

下面當我跑這POM然後我得到的結構是這樣

Project structure

dependency-jars folder

conf folder

現在我用下面的試了一下用渣滓罐任務

apply plugin: 'application' // implicitly apply java and distribution plugin 
apply plugin: 'eclipse' 

sourceCompatibility = 1.8 
targetCompatibility = sourceCompatibility 
mainClassName = "com.softech.ls360.integration.BatchImport" 
version = '1.0' 

ext { 
    log4jGroupId = "org.apache.logging.log4j" 
    springFrameworkGroupId = "org.springframework" 
    springFrameworkVersion = "4.2.4.RELEASE" 
    junitVersion = "4.12" 
    .... 
} 

dependencies { 
    compile group: log4jGroupId, name: 'log4j-api', version: log4jVersion 
    .... 
    runtime group: 'org.jboss.logging', name: 'jboss-logging', version: jbossLoggingVersion 
    .... 
    testCompile group: 'junit', name: 'junit', version: junitVersion 
} 

jar { 

    // Keep jar clean: 
    exclude '**/*.properties', '**/*.xml', '**/*.vm', '**/*.txt' 

    //from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 
    manifest { 
     attributes ('Implementation-Version': version, 
      'Main-Class': "$mainClassName", 
      'Class-Path': "conf/" + " " + configurations.runtime.files.collect {"dependency-jars/$it.name" }.join(' ') 
     ) 
    } 
} 

然後我得到了下面的jar

enter image description here

它是產生在罐子正確的類路徑。但build/libs/文件夾只包含jar,其中沒有dependency-jarsconf文件夾。我如何創建這些文件夾,所以當我運行任務jar時,它會在build/libs/文件夾中創建jar,同時創建dependency-jars文件夾以及其中的所有jar文件。並且conf文件夾中的build/libs,與所有.propeties, .xml, .vm, .txt文件從src/main/resources/文件夾中。

謝謝

回答

0

嗯我嘗試了以下,它的工作。我不知道這種方法有多好。但是如果有人有更好的方式去做,那麼請張貼答案。

.... 
task wrapper(type: Wrapper) { 
    gradleVersion = '2.10' 
} 

task copyJars(type: Copy) { 
    from configurations.runtime 
    into "$buildDir/libs/dependency-jars" 
} 

task copyConfigurationFiles(type: Copy) { 
    description 'Copies the configurations files src/main/resources directory (from) to the target directory(into).' 
    from "src/main/resources" 
    into "$buildDir/libs/conf" 
    include '**/*.properties', '**/*.xml', '**/*.vm', '**/*.txt' 
} 

task copyFiles(dependsOn: [copyJars, copyConfigurationFiles]) 

jar { 

    dependsOn copyFiles 

    // Keep jar clean: 
    exclude '**/*.properties', '**/*.xml', '**/*.vm', '**/*.txt' 

    manifest { 
     attributes ('Implementation-Version': version, 
      'Main-Class': "$mainClassName", 
      'Class-Path': "conf/" + " " + configurations.runtime.files.collect {"dependency-jars/$it.name" }.join(' ') 
     ) 
    } 
} 

之後,我只是運行任務jar。它創建了jar文件,以及其中包含所有jar文件的dependency-jars文件夾,還創建了conf文件夾以及其中的所有配置文件。就像我在做maven install

謝謝