How can I add a subproject referenced using project(':api') to the jar gradle builds?
This is the build.gradle of my main project. The subproject is includes as git submodule and has a similar buildscript.
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes('Main-Class': '..........')
}
}
repositories {
mavenCentral()
}
dependencies {
compile files('libs/jfxrt.jar')
compile project(':api')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I figured it out on my own.
Include the source of a subproject in the main jar:
sourceSets {
main {
java {
srcDir project(':api').file('src/main/java')
}
}
}
Including the classes of a jar in the main jar:
jar {
from zipTree('libs/abc.jar')
}
Try to add classpath to your manifest file. You need to have directory (example below uses "lib") to keep jar files on which your project depends.
Try modifying your "jar" block in gradle build to something like this. I have some addition properties just for demonstration. But the important one is Class-Path
jar {
manifest.attributes(
'Class-Path': lib/api.jar
'Built-By': System.getProperty('user.name'),
'Built-JDK': System.getProperty('java.version'),
'Built-OS': System.getProperty('os.name'),
'Built-DATE': buildDate,
)
}
I hope it helps to fix your issue.
In the simplest case, a fat Jar can be created as follows:
main/build.gradle:
jar {
from configurations.runtime
}
There are other, more robust solutions, such as the gradle-one-jar plugin for "main" method style applications.
Related
I have a local jar file named mylib.jar. I want to used it as a dependency in my Gradle Java project.
This is what I tried:
I created a libs/ folder under project root. I put the jar file under libs/ folder.
MyProject
->libs/mylib.jar
->build.gradle
->src/...
In my build.gradle:
apply plugin: 'java-library'
group 'com.my.app'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
api files('libs/mylib.jar')
}
But I can't access the public classes defined in mylib.jar in my project code. Why?
===== More information =====
The content of my jar:
mylib.jar
> com.my.jar.package
>ClassFromJar.class
Here is how I use the jar:
// Compilation error: Cannot resolve symobl 'ClassFromJar'
import com.my.jar.package.ClassFromJar;
public class MyEntryPoint {
// Compilation error: Cannot resolve symbol 'ClassFromJar'
ClassFromJar instance = new ClassFromJar();
}
Similar answers suggesting
Local dir
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs 'libs'
}
}
Relative path:
dependencies {
implementation files('libs/mylib.jar')
}
Use compile fileTree:
compile fileTree(dir: 'libs', include: 'mylib.jar')
A flatDir repository is only required for *.aar(which is an Android specific library format, completely irrelevant to the given context). implementation and api affect the visibility, but these are also Android DSL specific). In a common Java module, it can be referenced alike this:
dependencies {
compile fileTree(include: ["*.jar"], dir: "libs")
}
And make sure to drop the *.jar into the correct one libs directory, inside the module.
I thinks you should use in dependency declaration compile name: 'mylib' in this case flatDir will search for mylib.jar.
You could try following build.gradle (it works in my project):
plugins {
id 'java'
}
apply plugin: 'java-library'
group 'dependency'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile name: 'mylib'
}
Just in case:
1 - compiler must have access to lib directory and jar
example:
javac -cp .;lib\* *.java
2 - ALSO import must be mentioned in java file
example in your java add
import static org.lwjgl.glfw.GLFW.*;
I've problem with JXBrowser license file. I've followed https://jxbrowser-support.teamdev.com/docs/quickstart/gradle-config.html official guide to use with Gradle and this is my current build.Gradle
Build seem normal and no any error.
plugins {
id 'java'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
test {
java {
srcDir 'test'
}
}
}
jar {
manifest {
attributes 'Main-Class': 'main.Main'
}
}
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url = 'http://maven.teamdev.com/repository/products' }
}
ext {
jxBrowserVersion = '6.22'
}
dependencies {
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
compile "com.teamdev.jxbrowser:jxbrowser-cross-platform:${jxBrowserVersion}"
compile 'com.maxmind.geoip2:geoip2:2.12.0'
compile files("$rootDir/license.jar")
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Error happens when it run I'm not sure my build.Gradle is wrong or something here is error
Exception in thread "main" java.lang.NoClassDefFoundError: com/teamdev/jxbrowser/chromium/PermissionHandler
at main.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.chromium.PermissionHandler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
I suppose you see this exception when you run your Java application through an executable JAR file.
This exception indicates that you didn't include JxBrowser JAR files into your application class path. I see that you include JxBrowser JAR files as compile dependencies into your build.gradle. It's OK during build, but not for production.
Please make sure that you add JxBrowser JAR files into the classpath of your application. For example, you can download the required JAR files, put them into some directory, and configure the Class-Path attribute in your JAR file as show in the Oracle's tutorial.
Or you can use the Gradle plugin that includes all dependencies into a single fat JAR. In this case you don't need to configure Java app classpath.
Now it's work using JDK & JRE < 10
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'm trying to build a fat jar which would contain all .jar libraries included in 'libraries' folder.
Here is a snapshot of my build.gradle file:
group 'MyApp'
version '2.0'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile files('libraries/ojdbc7.jar')
compile files('libraries/postgresql-42.1.4.jar')
compile files('libraries/db2jcc.jar')
}
tasks.withType(Jar) {
destinationDir = file("$rootDir/target")
}
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'MainLauncher'
}
processResources{
exclude '*'
}
archiveName 'myapp.jar'
}
All .jar libraries are included into 'libraries' folder located in root of the project.
For some reason my code works fine in IDE, however fails to execute some tasks when run as a standalone .jar file.
When I decompress the jar, I can see all content inside root folder including libraries which fail to be found.
Is there anything I'm doing wrong?
I have eclipse java project.
Gradle script:
apply plugin: 'java'
jar {
manifest {
attributes 'Title': 'xxx'
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: '**/*.jar')
}
def myPackage = 'mypackage'
sourceSets {
main {
java.srcDirs = ['src']
}
}
Project file structure
src
com.company.core
....classes
com.company.impl
....classes
In jar file need file structure
com.company.core
...classes
com.mypackage.impl
...classes
Need to rename package
company -> mypackge in jar file
It is not android.
Help please.
UPD
Thanks, Lance Java.
It working for me.
But there was a problem. All classes from libs (google-play-services.jar, android.jar) come into jar... I need classes located only in src folder... I can to exclude classes by all packages, but i think it is no good solution...
Is there another way whith shadowJar?
My dependencies:
dependencies {
compile files('libs/android.jar')
compile files('libs/google-play-services.jar')
}
This can be done with the shadow plugin
See relocating packages