Gradle:如何创建多个jar? [英] Gradle: How to create multiple jar?

查看:228
本文介绍了Gradle:如何创建多个jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Gradle和Groovy的新手,我希望有一些东西可以解决我的问题。

I'm new to Gradle and Groovy, and I'd hope there would be something to solve my problem.

我有几个软件包,每个软件包都需要编译成一个jar。

我发现的一个解决方案是创建多个任务是 Jar 类型,但我想避免复制/粘贴,并在每次向项目添加新包时最终使用巨型gradle文件。

I have several packages, each of which needs to be compiled into one jar.
One solution I've found is to create multiple tasks that are of Jar type, but I'd like to avoid copy/paste and end up with a giant gradle file whenever I add a new package to my project.

我目前的实现是有多个jar任务,比如这个:

My current implementation is to have multiple jar tasks, like this one :

task jarFoo(type: Jar) {
    baseName = "foo"
    version = "1.0.0"
    String className = baseName.capitalize()

    from(sourceSets.main.output) {
        include "$baseName/**"
    }

    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    manifest {
        attributes "Implementation-Title": "$className",
                "Implementation-Version": "$version",
                "Main-Class": "$baseName.$className"
    }
}

它就像一个魅力,但我经常添加包,我最终会得到很多包,因此需要很多任务和大量的复制/粘贴代码。

It works like a charm, however I add packages very often and I will end up with a lot of packages, therefore a lot of tasks and a lot of copied/pasted code.

在摆弄 build.gradle 文件后,我发现我需要从<$ c $扩展c> Jar 以便创建一个jar。

After fiddling with build.gradle file, I've found that I needed to extend from Jar in order to get a jar created.

所以这是迄今为止该类的代码:

So here's the code for the class so far :

class JarTask extends Jar {
    String jarName = "default"
    String jarVersion = "1.0.0"

    @TaskAction
    def jar() {
        baseName = jarName
        version = jarVersion
        String className = baseName.capitalize()

        // Thanks Opal for reminding that sourceSets
        // is defined in project.
        from(project.sourceSets.main.output) {
            include "$baseName/**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$className",
                    "Implementation-Version": "$version",
                    "Main-Class": "$baseName.$className"
        }
    }
}

task jarFoo(type: JarTask) {
    jarName = "foo"
}

task jarBar(type: JarTask) {
    jarName = "bar"
    jarVersion = "1.2.42"
}

问题是创建的jar基本上忽略了方法中的所有内容:它只包含 MANIFEST.MF ,其中包含一行清单版本,并且给出了项目的名称,而不是任务中给出的名称。没有别的。

The problem is that the jar that is created ignores basically everything in the method: it contains only a MANIFEST.MF containing one line with the manifest version and is given the name of the project, not the name given in task. Nothing else.

如果需要,您可以在我的 GitHub回购(主要是法语)。

If needed, you can find the code online in my GitHub repo (mainly in French).

任何想法都会真的很感激!

Any idea would be truly appreciated!

推荐答案

这是另一个允许传递参数的简单选项。我在这个主题上找到了灵感: https:// discuss .gradle.org / t /传递参数到任务/ 8427/20 ,听起来和我试图做的完全一样。

Here is another easier option that allows to pass parameters. I found the inspiration on this topic : https://discuss.gradle.org/t/passing-arguments-to-a-task/8427/20, which sounds exactly like what I was trying to do.

在这里,我们基本上定义了一个返回任务的方法,给定了一些参数。其余的只是测试是否给出了版本,或者已经提供了相应的代码并使用@Opal很好的帮助。

Here we basically define a method that returns a task, given some parameters. The rest is just testing if a version is given, or the code already given in question and adapted with @Opal great help.

包含新版本就足够了工件阻止使任务可用。然后,只需运行 gradle jarqcm 来构建一个包或 gradle assemble 来编译所有内容。

It is sufficient to include the new builds in the artifacts block to make tasks available. Then, just run gradle jarqcm to build a single package or gradle assemble to compile everything.

apply plugin: "idea"
apply plugin: "java"

repositories {
    mavenCentral()
}

dependencies {
    compile "com.intellij:forms_rt:7.0.3"
    runtime "com.intellij:forms_rt:7.0.3"
}

def jarPackage(artifactName, artifactVersion) {
    if (artifactVersion == "" || artifactVersion == null) {
        artifactVersion = "1.0.0"
    }
    return tasks.create("jar${artifactName}", Jar) {
        baseName = artifactName
        version = artifactVersion
        String className = baseName.capitalize()

        from(sourceSets.main.output) {
            include "$baseName/**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$className",
                    "Implementation-Version": "$version",
                    "Main-Class": "$baseName.$className"
        }
    }
}

artifacts {
    archives jarPackage("aleatoire", ""), jarPackage("calculatrice", "1.2.3"), jarPackage("copier", ""),
            jarPackage("qcm", "1.0.0")
}

这篇关于Gradle:如何创建多个jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆