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

标签 gradle gradle-plugin gradle-eclipse

我是gradle的新手,正在尝试将我的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/文件。

谢谢

最佳答案

嗯,我尝试了以下方法,并且有效。我不知道这种方法有多好。但是,如果有人有更好的方法,那么也请发布答案。

....
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一样

谢谢

关于gradle - 转换maven jar并将资源插件复制到gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35267141/

相关文章:

flutter - 无法建立Flutter APK以发布

java - artifactoryPublish 配置中发布部分的魔力

gradle - Gradle插件- 'Could Not resolve all dependencies for configuration…'中的getArtifacts()结果

android - 在 Android Studio 2.1.3 中,实验性 gradle 不工作吗?

eclipse - 如何使用Gradle Kotlin DSL创建Eclipse项目?

gradle - gradle 项目之间的 eclipse gradle 工作区分辨率不起作用

android - 从 node_modules 目录导入 React-Native Android 不起作用

java - 带有Gradle和TestNG的FarmIntegrationTest无法运行?

Eclipse 中的 Gradle 未在 Eclipse IDE 中显示外部依赖项源和 java 文档

java - 如何将本地.jar文件依赖添加到build.gradle文件?