首页 > 解决方案 > gradle java library plugin不会将src/main/resources下的文件放到jar文件中

问题描述

我使用gradle java library pluginandmaven publish plugin创建一个 jar 文件,然后将其发布到一个 nexus 存储库。但是,生成的 jar 文件不包含该路径application.properties下的文件。src/main/resource我做了一些研究,显然 gradle 应该src/main/resource默认将路径下的文件添加到 jar 中。所以我不知道为什么不添加它。

我的项目结构

src
  --main
    --java
       --com.abc.bcd
          --A.java
          --B.java
          --C.java
    --resources
      --application.properties

我的 build.gradle 文件是这样的

/*
 * This file was generated by the Gradle 'init' task.
 */
apply plugin: 'java-library'
apply plugin: 'maven-publish'

repositories {
    mavenCentral()
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    classifier = 'sources'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation 'com.google.guava:guava:27.0-jre'
    api 'org.springframework.boot:spring-boot-starter-webflux:2.1.2.RELEASE'

    testImplementation "org.assertj:assertj-core:3.11.1"
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.1"
    testImplementation "com.github.tomakehurst:wiremock-standalone:2.19.0"
    testImplementation "org.junit.platform:junit-platform-surefire-provider:1.3.2"
    testImplementation "org.junit.platform:junit-platform-runner:1.3.2"
    testImplementation "ru.lanwen.wiremock:wiremock-junit5:1.2.0"
}

group = 'com.abc.bcd'
version = '0.0.1'

publishing {
    publications {
        metrics(MavenPublication) {
            from components.java
            artifact sourcesJar
        }
    }

    repositories {
        maven {
            credentials {
                username = "${nexus_user}"
                password = "${nexus_pass}"
            }
            url 'https://nexus.abc.io/repository/bcd-shared/content/repositories/snapshots'
        }
    }
}

jar {
    into("META-INF/maven/$project.group/$project.name") {
        from { generatePomFileForMetricsPublication }
        rename ".*", "pom.xml"
    }
}

生成的 jar 文件现在只包含 java 类

   com
     --abc
        --bcd
           --A.class
           --B.class
           --C.class
   META-INF
       --MANIFEST.MF
       --maven
          --abc.bcd
            --pom.xml

标签: javagradle

解决方案


推荐阅读