Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add file to a specific directory #2453

Closed
saturnism opened this issue May 6, 2020 · 7 comments
Closed

Ability to add file to a specific directory #2453

saturnism opened this issue May 6, 2020 · 7 comments
Milestone

Comments

@saturnism
Copy link

Description of the issue:
Some web apps require static files to be copied into resources' staticdirectory. When adding arbitrary files to Jib, it can be more useful to be able to specify target directory.

Similar to the way that a JAR is constructed: https:/oktadeveloper/okta-angular-deployment-example/blob/jar/notes-api/build.gradle.kts#L79

Current workaround is to copy the files into src/main/resources, but these are generated files and shouldn't go into src. See https:/oktadeveloper/okta-angular-deployment-example/blob/jar/notes-api/build.gradle.kts#L98

jib-gradle-plugin Configuration:
See https:/oktadeveloper/okta-angular-deployment-example/blob/jar/notes-api/build.gradle.kts#L98

Additional Information:

cc @mraible

@TadCordle
Copy link
Contributor

I think this covers what you're asking for: #1581

tl;dr - extraDirectories will allow something similar to the following:

maven:

<extraDirectories>
  <paths>
    <path>
      <from>src/main/custom-dir</from>
      <into>/target/on/container</into>
    </path>
    ...
  </paths>
</extraDirectories>

gradle:

jib {
  extraDirectories {
    paths {
      path {
        from = 'src/main/custom-dir'
        into = '/target/on/container'
      }
      ...
    }
  }
}

@TadCordle TadCordle added this to the v2.3.0 milestone May 6, 2020
@TadCordle
Copy link
Contributor

Closing as duplicate of #1581, feel free to comment on the existing issue if you have anything to add.

@mraible
Copy link

mraible commented May 6, 2020 via email

@mraible
Copy link

mraible commented May 6, 2020

@TadCordle No need to answer my questions. I figured out a solution with @saturnism's help. Here's my Gradle + Kotlin build for Spring Boot that uses Jib and creates an image with Angular files from a different directory.

import com.moowork.gradle.node.npm.NpmTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("se.patrikerdes.use-latest-versions") version "0.2.13"
    id("com.github.ben-manes.versions") version "0.28.0"
    id("org.springframework.boot") version "2.2.6.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    id("com.github.node-gradle.node") version "2.2.3"
    id("com.google.cloud.tools.jib") version "2.2.0"
    kotlin("jvm") version "1.3.61"
    kotlin("plugin.spring") version "1.3.61"
    kotlin("plugin.jpa") version "1.3.61"
}

val spa = "${projectDir}/../notes";

node {
    version = "12.16.2"
    nodeModulesDir = file(spa)
}

group = "com.okta.developer"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-data-rest")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("com.okta.spring:okta-spring-boot-starter:1.4.0")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    if (project.hasProperty("prod")) {
        runtimeOnly("org.postgresql:postgresql")
    } else {
        runtimeOnly("com.h2database:h2")
    }
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

val buildWeb = tasks.register<NpmTask>("buildNpm") {
    dependsOn(tasks.npmInstall)
    setNpmCommand("run", "build")
    setArgs(listOf("--", "--prod"))
    inputs.dir("${spa}/src")
    inputs.dir(fileTree("${spa}/node_modules").exclude("${spa}/.cache"))
    outputs.dir("${spa}/dist")
}

val profile = if (project.hasProperty("prod")) "prod" else "dev"

tasks.bootRun {
    args("--spring.profiles.active=${profile}")
}

tasks.processResources {
    rename("application-${profile}.properties", "application.properties")
    if (profile == "prod") {
        dependsOn(buildWeb)
        from("${spa}/dist/notes") {
            into("static")
        }
    }
}

jib {
    to {
        image = "mraible/bootiful-angular"
    }
    container {
        environment = mapOf("SPRING_PROFILES_ACTIVE" to profile)
    }
}

@TadCordle
Copy link
Contributor

Any chance there's a timeline for adding it as a feature?

I just started working on it recently, so hopefully it will be in the next release :)

@chanseokoh
Copy link
Member

chanseokoh commented May 21, 2020

@saturnism @mraible Jib 2.3.0 is released with this very handy feature. Now you can designate target directories with jib.extraDirectories.paths.path.into. See #1581 (comment) for an example.

@saturnism
Copy link
Author

Cool - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants