Basit February 2016

convert maven jar and copy resources plugin to gradle

I am new to gradle and trying to convert my POM into gradle.build. I have convert the dependencies part and now converting plugin part. I have following in my 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>
              

Answers


Basit February 2016

Hhmm I tried the following and it worked. I don't know how well this approach is. But if someone has better way to do it then please post answer too.

....
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(' ')
       )
    }
}

After that I simply run task jar. And it created the jar, as well as dependency-jars folder with all jars in it and also conf folder with all configuration files in it. Just like when I did maven install

Thanks

Post Status

Asked in February 2016
Viewed 2,192 times
Voted 5
Answered 1 times

Search




Leave an answer