想要使用 Gradle 将 repo 中的 .ebextensions (AWS Beanstalk) 目录复制到 Java jar 文件的根目录中

Posted

技术标签:

【中文标题】想要使用 Gradle 将 repo 中的 .ebextensions (AWS Beanstalk) 目录复制到 Java jar 文件的根目录中【英文标题】:Want to copy .ebextensions (AWS Beanstalk) directory in repo into the root directory of a Java jar file using Gradle 【发布时间】:2018-03-13 03:30:28 【问题描述】:

我有一个 Spring Boot 项目,我需要使用 Gradle 将我的 repo 中的 .ebextensions 文件夹复制到我的 jar 文件的 ROOT(***)文件夹中。如果不在 jar 文件的根目录下,我注意到 AWS Beanstalk 不会拾取 .ebextensions 文件夹下的 nginx conf 文件。

即这个文件夹目前在 repo 中:

src
build.gradle
gradlew.bat
gradlew
build
README
.ebextensions

在我的 build.gradle 中,我有这段代码:

jar 
   from('.')
   into("./.")
   include '.ebextensions/**'

但是,我发现 .ebextensions 文件夹最终会位于 jar 文件中的 BOOT-INF/classes/ 下。它还将擦除所有其他在 BOOT-INF/classes/ 下的其他类文件!

如何使我的目录与 BOOT-INF 处于同一级别?即像这样:

.
..
BOOT-INF
WEB-INF
.ebextensions
<Rest of the source files here>

谢谢!

附:我也尝试了下面的另一种解决方案,但它也不起作用:

task copyEbExtensions(type: Copy) 
  from '.'
  into  getDestDir() 
  include '.ebextensions'

P.S.#2 另外,这是我的 build.gradle 以防万一:

buildscript 
    repositories 
       mavenCentral()
    
    dependencies 
       classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE")
    


apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar 

   baseName = 'oneyearafter'
   version =  '0.1.26'




task copyEbExtensions(type: Copy) 
    from '.'
    into  getDestDir() 
    include '.ebextensions'


task wrapper(type: Wrapper) 
    gradleVersion = '2.0'



repositories 
    mavenCentral()


sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies 
    compile("org.springframework.boot:spring-boot-devtools")

    compile("org.springframework.boot:spring-boot-starter-web")
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    // end::actuator[]
    // tag::tests[]

    compile("org.thymeleaf:thymeleaf-spring4")

    compile("org.springframework.boot:spring-boot-starter-security")

    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'

    // Use mysql Connector-J
    compile 'mysql:mysql-connector-java'





bootRun 
    addResources = true

【问题讨论】:

您是否尝试将该文件推送到/java/main/resources 文件夹下? Gradle 会将资源下的所有文件复制到 jar 文件的根顶部。 嗨团霸,是的,我最初确实尝试过。这是一个 Spring Boot 项目,我最初在 src/main/resources/ 下有 .ebextensions 文件夹,但是当构建 jar 时,我在根级别根本看不到 .ebextensions 文件夹 我使用custome gradle函数构建jar文件,它可以将sources下的文件复制到root。我无法在评论中发布所有内容,因此我将在答案中发布。 【参考方案1】:

这是我构建 jar 文件的自定义 gradle 函数。运行任务compileRunableJarFile编译jar文件。

apply plugin: 'java'

sourceSets 
    main 
        java 
            srcDir 'src/java'
        
        resources 
            srcDir 'src'
            exclude 'src/java/**/*'
        
    


compileJava.dependsOn(processResources)
jar 
    duplicatesStrategy = 'exclude'
    manifest 
        attributes 'Implementation-Title': title,
                'Implementation-Version': version,
                'Implementation-Vendor': vendor,
                'Created-By': creator,
                'Main-Class': mainClass,
                'Manifest-Version': version,
                'Manifest-Title': title,
                'Application-Name': title,
                'JPA-PersistenceUnits': persistenceUnit
    


//create a single Jar with all dependencies
task compileRunableJarFile(type: Jar, description: 'to create runable JAR.', group: 'compile') 
    manifest 
        attributes 'Implementation-Title': title,
                'Implementation-Version': version,
                'Implementation-Vendor': vendor,
                'Created-By': creator,
                'Main-Class': mainClass,
                'Manifest-Version': version,
                'Manifest-Title': title,
                'Application-Name': title,
                'JPA-PersistenceUnits': persistenceUnit
    
    baseName = 'app_name_prefix-' + getCurrentTime()
    from  configurations.compile.collect  it.isDirectory() ? it : zipTree(it)  
    with jar

【讨论】:

您好 Tuan,感谢您的回答,我将尝试根据您发布的内容修改 build.gradle 以查看它是否有效。我还将我当前的 build.gradle 粘贴到我原来的问题中,以防你也对它感兴趣

以上是关于想要使用 Gradle 将 repo 中的 .ebextensions (AWS Beanstalk) 目录复制到 Java jar 文件的根目录中的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 发布到 Artifactory 的特定 repo

未知主机'repo.maven.apache.org'。您可能需要在 Gradle 中调整代理设置

Gradle 无法通过企业代理连接到 maven repo - 需要通过 Sencha/Cordova 进行配置

Gradle 无法解析 org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE (repo1.maven.org: Nome

Gradle:如何将带有子路径的 sourceControl 定义为依赖项?

Gradle 构建错误:无法从 https://repo1.maven.org/maven2/io/fabric/tools/gradle/maven-metadata.xml 加载 Maven 元数