Gradle multiple jars from single source folder

后端 未结 4 1267
执笔经年
执笔经年 2020-12-07 18:04

As for now we have a project structure with single source folder named src, which contains source code for three modules. What I want to do is:

1) Comp

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 18:22

    I agree in principal with the accepted answer too. I found a project where the client requires two JAR essentially of the same file except the Manifest is different only by the Class-Path key.

    jar {
        manifest {
            attributes(
                    "Main-Class": platformMainClass,
                    "Implementation-Title": platformDisplayName,
                    "Implementation-Description": platformDescription,
                    "Platform-Version": platformVersion,
                    "Implementation-Version": version,
                    "Build-Assembly-User": System.getProperty("user.name"),
                    "Build-Assembly-Date": new java.util.Date().toString(),
                    "Class-Path": configurations.compile.collect { "lib/"+it.getName() }.join(' ')
            )
        }
    
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    
        exclude( [ 'log4j*.properties', 'uk/gov/acme/secret/product/server/**' ])
    }
    

    The same manifest and the source code then is:

    task applicationClientJar(type: Jar, description: "Creates the Application  Client JAR file.") {
        dependsOn compileJava
        manifest {
            attributes(
                    "Main-Class": platformMainClass,
                    "Implementation-Title": platformDisplayName,
                    "Implementation-Description": platformDescription,
                    "Platform-Version": platformVersion,
                    "Implementation-Version": version,
                    "Assembly-Date": new java.util.Date().toString()
            )
        }
        archiveName = "acme-client-${platformVersion}.jar"
        destinationDir = file("${buildDir}/libs")
        from sourceSets.main.output
    
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    
        exclude( [ 'log4j*.properties', 'uk/gov/acme/secret/product/server/**'     }
    

    So Grzegorz notation is correct, because the Gradle should know there are two different JAR with GAVs. Multi-module is the preferred option.

    compile "uk.gov.acme.secret:acme:1.0"  // CORE
    compile "uk.gov.acme.secret:acme-client:1.0"
    

    The only way to configure for this is to use the Multi-Module Gradle project and then add a compile and/or deploy dependency to the core / main project.

    project(':common:acme-micro-service-webapp') {
        dependencies {
            compile project(':common:acme-core')
        }
    }
    

    Inside the 'acme-micro-service-webapp' project, this ensures that the dependent 'common:acme-core' is compiled first.

    PS: I am still trying to figure out a better solution.

    PS PS: If you are using Maven as well as, it may be possible to hook on the `install' task.

提交回复
热议问题