转换maven jar并将资源插件复制到gradle

时间:2016-02-08 10:17:02

标签: gradle gradle-plugin gradle-eclipse

我是新手,并试图将我的POM转换为gradle.build。我已转换依赖项部分,现在转换插件部分。我在POM中关注了

<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

现在我尝试使用以下gradle jar任务

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

它在jar中生成适当的类路径。但build/libs/文件夹仅包含jar,其中没有dependency-jarsconf个文件夹。如何创建这些文件夹,因此当我运行任务jar时,它会在build/libs/文件夹中创建jar,同时创建包含所有jar的dependency-jars文件夹。 conf中的build/libs文件夹,以及.propeties, .xml, .vm, .txt文件夹中的所有src/main/resources/个文件。

由于

1 个答案:

答案 0 :(得分: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

时一样

谢谢