I have a library project in Java which is several folders, each one doing specific parts and having its own dependencies.
Since I am working locally I would like to deploy this library locally and get the Jar to import to another project.
For this reason I am using gradle and what I did was going to the directory where I have all the folders of the library and gradle init and then gradle build.
Since I want the files locally, I saw that I can use gradle publishToMavenLocal, which I did and it created a jar file under ~/.m2/..... Now the issue is that this jar file appear to only contain a META-INF folder and inside of it a manifest.mf file.
This is the build.gradle file used.
What am I doing wrong? Should I do something different?
check gradle docs
there is also a complete example.
be sure to add your sourceSets that you want to compile and build in the jar.
build.gradle
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets {
main {
java { srcDir 'src/main/java' }
resources {
srcDirs 'src/main/resources'
}
}
test {
java { srcDir 'src/test/java' }
resources {
srcDirs 'src/test/resources'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'project1-sample'
version = '1.1'
from components.java
}
}
}
You could also add your library project to your main project like this :
build.gradle
dependencies {
compile project(':library_project')
}
settings.gradle
rootProject.name = 'Project'
include ":library_project"
project(':library_project').projectDir = new File(settingsDir, '../library_project')
Related
I been working on a project that uses an external jar file. Whenever, I build my project using build.grade, and the external files does not get added to the final jar file. Inside my workspace, I have a "libs" folder which holds the external jar file (Discord Rich Presence). Can anyone help me sort this out?
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
version = "1.0"
group= "com.yourname.modid"
archivesBaseName = "example"
minecraft {
version = "1.8.9-11.15.1.1722"
runDir = "run"
mappings = "stable_20"
}
repositories {
flatDir {
dirs "libs"
}
}
dependencies {
compile name: 'discord-rpc'
}
processResources {
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
expand 'version':project.version, 'mcversion':project.minecraft.version
}
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
The default task jar only packages the files of project source code. To include all the dependencies inside the JAR file, you could try https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow:
plugins {
...
id "com.github.johnrengelman.shadow" version "7.0.0"
}
...
Run this command to package all:
$ ./gradlew shadowJar
I am using gradle to package some java code into a jar. I am using some classes from tools.jar. I have had success in gradle building it and making a jar, but when I run that jar using java -jar <package>.jar I get the folowing
java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine.
Since tools.jar is something you get with a jdk, not a jre. Is there a way I can bundle tools.jar with my package.jar and have my jar work anywhere?
Here is my build.gradle so far.
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
}
description = "A java program"
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
flatDir {
dirs System.properties['java.home'] + '/../lib'
}
}
jar {
archiveName = "jProg.jar"
manifest {
attributes(
'Dependencies': 'com.sun.tools'
)
}
}
dependencies {
compile group: 'com.sun', name: 'tools'
}
Probably what you need is called 'fat jar' (Gradle packs all dependencies to single jar)
I am a gradle beginner and I am struggling to include the frontend distribution build folder in the backend jar (I use Spring Boot and the frontend is an ionic app). In the backend.gradle, I configured the jar-Task that should include the frontend-build folder (called www) into build folder of the backend. The jar task runs through, but the desired artifacts are not present in the backend-build folder and therefore not in the final jar. Would be glad for any help.
project structure:
project
build.gradle
settings.gradle
backend
--> backend.gradle
frontend
--> frontend.gradle
settings.gradle
include 'backend'
include 'frontend'
rootProject.children.each {
it.buildFileName = it.name + '.gradle'
}
build.gradle
allprojects {
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'idea'
repositories {
mavenCentral()
}
}
frontend.gradle
plugins {
id "com.moowork.node" version "1.2.0"
}
task clean(dependsOn: 'npm_run_clean') {
}
task build(dependsOn: 'npm_run_build') {
}
backend.gradle
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = 'ch.renewinkler'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
jar {
from('frontend/www') {
into('public')
}
}
processResources.dependsOn(':frontend:build')
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
You need to tell gradle that the jar task depends on the frontend's build task, otherwise it could run the jar file before the build task, and thus have nothing to include in the jar.
It's also a better idea to refer to projects by their name, instead of using absolute paths:
jar {
dependsOn(':frontend:build')
into('public') {
from "${project(':frontend').projectDir}/www"
}
}
I have a Spring Boot project, and I need to copy the .ebextensions folder from within my repo into the ROOT(top-level) folder of my jar file using Gradle. If it's not in the root directory of the jar file, I noticed that AWS Beanstalk will not pick up the nginx conf file under the .ebextensions folder.
i.e. this folder is currently here in the repo :
src
build.gradle
gradlew.bat
gradlew
build
README
.ebextensions
In my build.gradle, I have this code :
jar {
from('.')
into("./.")
include '.ebextensions/**'
}
But, I find out that the .ebextensions folder will end up under BOOT-INF/classes/ in the jar file. And it will also wipe all other class files that would otherwise be under BOOT-INF/classes/ too!
How do I get my directory on the same level as BOOT-INF? i.e. like this :
.
..
BOOT-INF
WEB-INF
.ebextensions
<Rest of the source files here>
Thanks!
P.S. I have also tried another solution below but it doesn't work either :
task copyEbExtensions(type: Copy) {
from '.'
into { getDestDir() }
include '.ebextensions'
}
P.S.#2 Also, this is my build.gradle in case this is helpful :
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
}
Here is my custom gradle function to build jar file. Run task compileRunableJarFile to compile jar file.
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
}
I'm new to Kotlin and Gradle, and tried to follow these steps, so I got the following 2 files:
after running gradle init I changed the build.gradle to be:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'application'
mainClassName = "hello.main"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Hello.kt:
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
Then I run the gradle build and got the build\classes\main\hello\HelloKt.class
my question is: Why the file generated is .class not .jar and how to get the .jar file and how to run it, I tried running the generated file using kotlin -classpath HelloKt.class main but got an error error: could not find or load main class hello.main
The classes are the direct output of the Kotlin compiler, and they should be packaged into a JAR by Gradle afterwards. To build a JAR, you can run the jar task, just as you would in a Java project:
gradle jar
This task is usually run during gradle build as well, due to the task dependencies.
This will pack the Kotlin classes into a JAR archive (together with other JVM classes, if you have a multi-language project), normally located at build/libs/yourProjectName.jar.
As to running the JAR, see this Q&A for a detailed explanation: (link)
Thanks for #hotkey answer, it helped me going the correct way.
First of all there is a mistake in the main class declaration, as it should follow the new methodology, that is in the below format:
mainClassName = '[your_namespace].[your_arctifact]Kt'
namespace = package name
arctifact = file name
so, considering the names given in the example above where filename is: Hello.kt, and the namespace is hello, then:
mainClassName = `[hello].[Hello]Kt`
using the previous method, that contains:
apply plugin: 'application'
mainClassName = 'hello.HelloKt'
the generated .jar file is not including the kotlin runtime, so the only way to execute it, is by:
d:/App/build/libs/kotlin -cp App.jar hello.HelloKt
but in order to generate a self contained jar that can be self-executed, and contains the kotlin runtime then the build.gradle should be written as:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes 'Main-Class': 'hello.HelloKt'
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
followed by gradle build, the [your_working_folder].jar file will be generated at the build/libs folder, assuming the working folder name is app, then file app.jar will be generated.
To run this file, one of the following 2 commands can be used:
D:\App\build\libs\java -jar App.jar
OR
D:\App\build\libs\kotlin App.jar hello.HelloKt