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
Related
I'm trying to port a legacy Java webapp project into gradle.
This is a snippet of my build.gradle
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
compile fileTree(dir: 'build/classes', include:'**')
}
To maintain the custom structure I want to put all my *.class files under WEB-INF/classes and it works, but I find also the same *.class files under WEB-INF/lib.
My goal it to keep jars and classes in separated war folder.
Any thoughts?
Edit: Added dependencies{} to the build.gradle snippet.
Problem get solved with commenting out builded classes from the dependencies:
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
// compile fileTree(dir: 'build/classes', include:'**')
}
We have a java project with Gradle multi build project. The project is big and we have many modules. We have our iml files under version control.
The environment is working until we are changing something in gradle files and importing gradle. Then it will remove all of our iml files. Sometimes it puts them directly under Locally Deleted Files but sometimes it is asking us that it will remove the orphan modules, and we can restore them if we want. At the same time it creates new iml files. Our iml files names have just one word as name but Gradle creates iml files with whole path of modules as this projectName.subfolder.module name.
We have saved many runConfiguration in project.ipr which needs correct module name, so they works. I tried once to remove all of our iml files and let gradle to create iml files and I changed our runConfiguration in ipr file to use the iml file names created by gradle. Then next time while importing gradle removed again all files generated by it and add the root directory name at beginning of iml files.
So if iml files name created by it first time was projectname.subdirectory.modulename it created a new iml files with this name rootDirectory.projectname.subdirectory.modulename. And when I changed the module names in the run configuration name to the new name then when importing gradle again it removed the rootDirectory from the name of iml files.
My question is if there is a way to configure gradle so it doesn't create new modules? If not is there a way to give the iml file names to the gradle or force it to specific names which doesn't changes.
We are using latest Intellij 2020.1 andwe have gradle wrapper with gradle-6.0.1. Our gradle settings in intellij looks like this:
I have tested to check the option Generate *iml files and uncheck it. But there is no differences it is working at the same way.
The definition of sourcesets looks like this in main build.gradle for whole project:
subprojects {
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testImplementation 'org.junit.platform:junit-platform-runner:1.4.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.4.2'
testImplementation 'org.hamcrest:hamcrest-library:1.3'
}
configurations {
runtimeLibraries
}
sourceSets {
main {
java {
srcDirs = ["src"]
}
resources {
srcDirs = ["resources"]
}
}
test {
java {
srcDirs = ["test/src"]
}
resources {
srcDirs = ["test/resources"]
}
}
}
}
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')
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
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.