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

Gradle Support/Example #14

Open
38leinaD opened this issue Sep 6, 2018 · 13 comments
Open

Gradle Support/Example #14

38leinaD opened this issue Sep 6, 2018 · 13 comments

Comments

@38leinaD
Copy link

@38leinaD 38leinaD commented Sep 6, 2018

When using kumuluzee from Maven, there is a plugin that packages the application/jar in the right way.
There should also be a plugin for Gradle or at least a gradle sample project that shows how to package the jar in the correct way for kumuluzee.

Based on my first tries, it seems not to be as simple as a shaded jar where all kumuluzee jars are just shaded in the application-jar. When comparing the output of the maven build, it seems, that some select classes are in the main application-jar and the other dependencies are contains as Jars.
It would be good to have a gradle build example that sets this up in exactly the right way.

@MBJuric
Copy link
Member

@MBJuric MBJuric commented Sep 7, 2018

We are planning to add Gradle support in the near future.

@daggerok
Copy link

@daggerok daggerok commented Nov 21, 2018

please let me know what is wrong with that gradle build script?

plugins {
  id 'java'
  id 'application'
  id 'com.github.johnrengelman.shadow' version '4.0.2'
}
version '1.0.0'
group 'com.github.daggerok'
mainClassName = 'com.kumuluz.ee.EeApplication'
shadowJar.mustRunAfter jar
defaultTasks 'shadowJar'
repositories {
  mavenCentral()
}
dependencies {
  compile 'com.kumuluz.ee:kumuluzee-core:3.0.0'
  compile 'com.kumuluz.ee:kumuluzee-servlet-jetty:3.0.0'
  compile 'com.kumuluz.ee:kumuluzee-cdi-weld:3.0.0'
  compile 'com.kumuluz.ee:kumuluzee-jax-rs-jersey:3.0.0'
  compile 'com.kumuluz.ee:kumuluzee-json-p-jsonp:3.0.0'
}

gradle shadowJar task can build uber jar file, but when I'm running it, I'm getting that error:

# ...skipped
2018-11-21 22:24:14.345 INFO -- com.kumuluz.ee.jetty.JettyFactory -- Starting KumuluzEE on Jetty with 5 minimum and 100 maximum threads
2018-11-21 22:24:14.375 INFO -- com.kumuluz.ee.jetty.JettyFactory -- Starting KumuluzEE on port(s): 8080 [http/1.1]
2018-11-21 22:24:14.427 INFO -- com.kumuluz.ee.jetty.JettyServletServer -- Starting KumuluzEE with context root '/'
2018-11-21 22:24:14.431 INFO -- com.kumuluz.ee.EeApplication -- Initializing components
2018-11-21 22:24:14.431 INFO -- com.kumuluz.ee.EeApplication -- Found EE component CDI implemented by Weld
2018-11-21 22:24:14.431 INFO -- com.kumuluz.ee.cdi.CdiComponent -- Initiating Weld
2018-11-21 22:24:14.431 INFO -- com.kumuluz.ee.EeApplication -- Components initialized
2018-11-21 22:24:14.431 INFO -- com.kumuluz.ee.EeApplication -- Initializing extensions
2018-11-21 22:24:14.432 INFO -- com.kumuluz.ee.EeApplication -- Extensions Initialized
2018-11-21 22:24:14.433 INFO -- org.eclipse.jetty.server.Server -- jetty-9.4.z-SNAPSHOT; built: 2018-06-05T18:24:03.829Z; git: d5fc0523cfa96bfebfbda19606cad384d772f04c; jvm 1.8.0_192-b12
2018-11-21 22:24:14.507 WARNING -- org.eclipse.jetty.webapp.WebAppContext -- Failed startup of context o.e.j.w.WebAppContext@5ed828d{/,file:///private/var/folders/p9/kwk2r83x3hldqvc998h02qkw0000gp/T/kumuluzee-tmp-webapp299415749452541202/,STARTING}
java.lang.ExceptionInInitializerError
        at org.eclipse.jetty.http.MimeTypes$Type.<init>(MimeTypes.java:103)
        at org.eclipse.jetty.http.MimeTypes$Type.<clinit>(MimeTypes.java:58)
        at org.eclipse.jetty.http.MimeTypes.<clinit>(MimeTypes.java:191)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:832)
        at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
        at org.eclipse.jetty.server.Server.start(Server.java:419)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
        at org.eclipse.jetty.server.Server.doStart(Server.java:386)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at com.kumuluz.ee.jetty.JettyServletServer.startServer(JettyServletServer.java:81)
        at com.kumuluz.ee.EeApplication.initialize(EeApplication.java:368)
        at com.kumuluz.ee.EeApplication.<init>(EeApplication.java:73)
        at com.kumuluz.ee.EeApplication.main(EeApplication.java:85)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at org.eclipse.jetty.http.PreEncodedHttpField.<clinit>(PreEncodedHttpField.java:71)
        ... 17 more

seems like problem with org.eclipse.jetty.http.PreEncodedHttpField, but why?

what I'm trying to configure is this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.daggerok</groupId>
  <artifactId>kumuluzee-maven-gradle-app</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.kumuluz.ee</groupId>
        <artifactId>kumuluzee-bom</artifactId>
        <version>3.0.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.kumuluz.ee</groupId>
      <artifactId>kumuluzee-core</artifactId>
    </dependency>
    <dependency>
      <groupId>com.kumuluz.ee</groupId>
      <artifactId>kumuluzee-servlet-jetty</artifactId>
    </dependency>
    <dependency>
      <groupId>com.kumuluz.ee</groupId>
      <artifactId>kumuluzee-cdi-weld</artifactId>
    </dependency>
    <dependency>
      <groupId>com.kumuluz.ee</groupId>
      <artifactId>kumuluzee-jax-rs-jersey</artifactId>
    </dependency>
    <dependency>
      <groupId>com.kumuluz.ee</groupId>
      <artifactId>kumuluzee-json-p-jsonp</artifactId>
    </dependency>
  </dependencies>
  <build>
    <defaultGoal>clean package</defaultGoal>
    <plugins>
      <plugin>
        <groupId>com.kumuluz.ee</groupId>
        <artifactId>kumuluzee-maven-plugin</artifactId>
        <version>${kumuluz.version}</version>
        <executions>
          <execution>
            <id>package</id>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Thanks


Regards,
Maksim

@urbim
Copy link
Member

@urbim urbim commented Nov 23, 2018

Hi!

kumuluzee-maven-plugin packages Uber JAR in a custom way, among other things, it uses kumuluzee-loader to bootstrap application. If you want to replicate that behavior in Gradle I think your best option would be to observe Uber JAR created with Maven and try to replicate that with Gradle.

@kustrun
Copy link

@kustrun kustrun commented Feb 2, 2019

Hi!

Do you happen to have any further guidelines how to use kumuluzee maven build task with Gradle? Or a possible support for Gradle?

@ArtificialPB
Copy link

@ArtificialPB ArtificialPB commented Feb 7, 2019

@kustrun and I managed to get it working with Gradle, replicating the Uber JAR structure generated by maven plugin. Here's the minimal required build.gradle example (run buildKumuluzee task).

configurations {
    kumuluzeeLoader { transitive = false }
}

dependencies {
    kumuluzeeLoader 'com.kumuluz.ee:kumuluzee-loader:3.1.0'
}

task repackageKumuluzee {
    /*Unpack and copy kumuluzee loader*/
    copy {
        from zipTree(configurations.kumuluzeeLoader.singleFile).matching { include "**/*.class" }
        into "$buildDir/classes/java/main"
        outputs.upToDateWhen { false }
    }
    /*Create boot loader properties*/
    file("$buildDir/classes/java/main/META-INF/kumuluzee").mkdirs()
    file("$buildDir/classes/java/main/META-INF/kumuluzee/boot-loader.properties").text = "main-class=com.kumuluz.ee.EeApplication"
}
task buildKumuluzee {
    jar {
        baseName = "kumuluz-app"
        archiveName = "${baseName}-${version}.${extension}"
        manifest {
            attributes "Main-Class": "com.kumuluz.ee.loader.EeBootLoader"
        }
        /*Copy project dependencies into lib folder as jars*/
        into("lib") {
            from configurations.runtimeClasspath
        }
    }
    dependsOn repackageKumuluzee
    finalizedBy build
}
@kustrun
Copy link

@kustrun kustrun commented Feb 8, 2019

In order to run different main class you have to edit this boot-loader property:
file("$buildDir/classes/java/main/META-INF/kumuluzee/boot-loader.properties").text = "main-class=com.kumuluz.ee.EeApplication"

jar -> manifest -> attributes -> main-class property MUST NOT BE CHANGED!

@MBJuric
Copy link
Member

@MBJuric MBJuric commented Feb 8, 2019

@ArtificialPB , @kustrun that's great. Would you be willing to contribute the complete Gradle build solution as a part of the kumuluzEE project? Much appreciated!

@ArtificialPB
Copy link

@ArtificialPB ArtificialPB commented Feb 9, 2019

@MBJuric Sure, should we make a PR with the above gradle build example added to the main repository readme file? https://github.com/kumuluz/kumuluzee

@MBJuric
Copy link
Member

@MBJuric MBJuric commented Feb 10, 2019

@ArtificialPB Yes, please. Also, if you could provide a markdown page that we would include in our Wiki (similar to this one: https://github.com/kumuluz/kumuluzee/wiki/Uber-JAR-support) - "Gradle support".
Also, a full sample in Gradle (such as JAX-RS sample) would be much appreciated, we would publish it in https://github.com/kumuluz/kumuluzee-samples
Thanks.

@ArtificialPB
Copy link

@ArtificialPB ArtificialPB commented Feb 13, 2019

@MBJuric Please find the JAX-RS gradle sample in the following repository: https://github.com/ArtificialPB/kumuluzee-jax-rs-gradle

And the Wiki page: https://gist.github.com/ArtificialPB/fa629f321fd3f9d74cc77078da4b4bab

@jeusdi
Copy link

@jeusdi jeusdi commented Mar 28, 2019

I've used this gradle file in order to build my uber jar.

I'm getting this message:

imagen

@daggerok
Copy link

@daggerok daggerok commented May 3, 2019

Thank you, @ArtificialPB

I can confirm that I was able use your solution to build kumuluzee fat jar with gradle!

It was little bit challenged migrate currect solution to Gradle Kotlin DSL, so I will left it here just in case if someone will be looking into it:

val kumuluzeeLoader by configurations.creating {
  extendsFrom(configurations["runtime"])
  setTransitive(false)
}

val kumuluzeeVersion: String by project

dependencies {
  kumuluzeeLoader("com.kumuluz.ee:kumuluzee-loader:$kumuluzeeVersion")
  // dependencies
}

tasks {
  register("kumuluzLoader", Copy::class.java) {
    group = "KumuluzEE"
    description = "Add KumuluzEE boot loader"

    from(zipTree(configurations["kumuluzeeLoader"].singleFile).matching {
      include("**/*.class")
    })
    into("$buildDir/classes/java/main")
    outputs.upToDateWhen { false }

    doLast {
      val filename = "$buildDir/classes/java/main/META-INF/kumuluzee"
      file(filename).mkdirs()
      file("$filename/boot-loader.properties")
          .writeText("main-class=com.kumuluz.ee.EeApplication\n", StandardCharsets.UTF_8)
    }
  }

  register("kumuluzJar", Jar::class.java) {
    group = "KumuluzEE"
    description = "Build KumuluzEE uber (fat) jar"

    archiveClassifier.set("all")
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
      attributes("Main-Class" to "com.kumuluz.ee.loader.EeBootLoader")
    }

    from(configurations.runtimeClasspath.get()) {
      into("lib") // dependencies
    }

    from(sourceSets.main.get().output) // app

    shouldRunAfter("kumuluzLoader")
    dependsOn("kumuluzLoader")
  }
}

defaultTasks("kumuluzJar")

Regards,
Maksim

@jeusdi
Copy link

@jeusdi jeusdi commented Jul 22, 2019

Is there any roadmap to publish an gradle plugin?

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

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.