Currently my gradle task looks like:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
...
}
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives sourcesJar
}
But in result jar artifact has *.java and *.kt files, but how to generate jar artifact with *.class files?
From command line, you just type:
jar uf0 path_to_your_jar_file\yourJarFile.jar path_to_your_classes\*.class
and of course, you can use the class name instead of a wildcard (*) if you don't want to add all classes from that directory.
jar --help
will give you more options.
Based on your screenshot, you are opening xxx-sources.jar file. Of course it contains only Java source file inside. This is the correct result as you configure as below:
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
You classes file normally are separated in another jar file. You didn't specify what are the tasks you executed. So let's assume you run the default Android build task, it will generate the aar file, it should contain your module classes file. You can unzip sapi-1.6.aar to check.
You can refer to this doc as well.
Updated:
You didn't specify why you need the Jar file instead of AAR file, let's assume you want to use Jar file in other project, then you need to use other gradle plugin to build (can't use android gradle plugin), it could be java or base gradle plugin. Some answer saying, you can find the jar file inside aar file, you can check this.
Related
I was told to include this in my build.gradle file in exercism.io in order to run my test suite in Java.
jar {
manifest {
attributes 'Main-Class': 'net.petrikainulainen.gradle.HelloWorld'
}
}
What is jar? What is the attributes 'Main-Class' doing? Is it specifying where my main class exists in the build folder?
In essence, gradle seems to be doing a few things for me:
1. creating a build folder where my Javascript compiled code exists
2. running my test suite
3. fetching any dependencies that I specify in the build.gradle file.
Is this typically what a dependency manager does?
In order to run tests you don't need to add a Main-Class attribute. What you've done there is specify that when building a jar file, include the Main-Class attribute in the jars manifest, which is the class to use as the entry when running a jar file via java -jar.
In answer to your second question, Gradle isn't a dependency manager, it's a build automation tool and yes, that's typically what they do for you.
I've successfully configured my gradle build script to create a zip distribution of my application with an extra 'config' folder at the root. This folder contains (at least right now) only one properties file in use by the application, and is on the classpath for the application.
What I'm looking for now, however, is a way to do the same with the 'run' task in the application plugin. When I try to run my application this way, (for testing), my program fails to run because of a class trying to access this properties file on the root of the classpath.
A bonus would be if I could get IntelliJ or Eclipse to also add this folder to its classpath just like the other folders (src/main/java, src/main/resources, ...) so I can run and debug my code from within the IDE without invoking a gradle task. I want to try to avoid as much as possible tying this code to any one IDE, so that when anybody needs to work on the project, they just need to import the build.gradle file and have the IDE make the appropriate config files it needs.
Here is my build.gradle file:
apply plugin: 'application'
mainClassName = "MainClass"
startScripts {
// Add config folder to classpath. Using workaround at
// https://discuss.gradle.org/t/classpath-in-application-plugin-is-building-always-relative-to-app-home-lib-directory/2012
classpath += files('src/dist/config')
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\config', '%APP_HOME%\\config')
unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/config', '$APP_HOME/config')
}
}
repositories {
...
}
dependencies {
...
}
Likely what needs to happen is that I need to have the /src/dist/config folder to be copied into the build directory and added to the classpath, or have its contents be copied into a folder that is already on the classpath.
I ended up taking Opal's suggestion as a hint, and came up with the following solution. I added the following to my build.gradle file:
task processConfig(type: Copy) {
from('src/main/config') {
include '**/*'
}
into 'build/config/main'
}
classes {
classes.dependsOn processConfig
}
run {
classpath += files('build/config/main')
}
Alternatively, a simpler approach would be to add a runtime dependency to my project as such:
dependencies {
...
runtime files('src/main/config')
}
I didn't end up doing it this way, however, because my distribution package ended up having .properties files in the lib folder... and I'm just picky that way.
As you can see in the docs run is a task of type JavaExec. So classpath for it can be modified. Try to add config folder to the classpath. See here.
I have a default Libgdx Gradle setup, and I need to add my simple text rendering library to it. It consists of a .jar file and native lib file.
This line of build.gradle script seems to work as I would expect, and what it does is add jfreetype.jar java library to my build path.
compile files('../local_lib/jfreetype.jar')
Is there a magic command like this to add native library (.dll to be exact) that is available on my file system and is not Mavenized?
natives "../local_lib/jfreetype32.dll"
This line of code just gives me an error saying that something cannot be found at some repo. I guess there should be a magical line like with .jar file to add native files that are available only on my file system and not on some repo.
The Gradle Natives plugin should do what you want.
You can specify a configuration that points at jar files that contain native dll/so. A gradle task "unpackNatives" will then unpack the dll/so into the build dirs.
Depending upon how you launch your application, you may still need to tell the Java runtime where to find the dll/so. There is some info about how this works at the project website:
https://github.com/cjstehno/gradle-natives
You can add a flat directory as a repository in this way, as mentioned in the dependency-management section in the Gradle User Guide.
repositories {
flatDir {
dirs '../local_lib'
}
}
If you want to create your own dependency-configuration natives, create it like this (more info on the same page):
configurations {
natives
}
Hope that helps.
I was trying to create two separate jar files with a single build.gradle.
It is almost working but one will overwrite the other. What's the right way of doing it?
jar {
//include contents of output dir
from "$buildDir/classes/main"
exclude '**/a1/**'
archiveName "X1-1.0.0.jar"
}
jar {
//include contents of output dir
from "$buildDir/classes/main"
exclude '**/a2/**'
archiveName "X2-1.0.0.jar"
}
Thanks
Rather than configuring the same Jar task twice, you need to use two Jar tasks. Assuming you are using the java plugin, you can reuse the Jar task named jar added by that plugin, and add another one yourself:
// reconfigure the Java plugin's `jar` task
jar {
exclude '**/a1/**'
baseName = 'X1'
}
// need to configure this one from scratch
task x2jar(type: Jar) {
// referring to the output in this way
// allows Gradle to infer task dependencies automatically
from sourceSets.main.output
exclude '**/a2/**'
baseName = 'X2'
}
// one way to make `gradle build` run both Jar tasks
assemble.dependsOn(x2jar)
For details on the configuration options, refer to the Gradle Build Language Reference.
I'm trying to figure out compiling a folder with Java source files in it.
There's a folder structure (root folders are org.AppName.i18n and META-INF). Eventually I need to get a Jar with the same structure and .java's converted to .class'es. Any hints how to make it? I'm on Windows.
You can use Ant for this: http://ant.apache.org/
A build tool, like Gradle is perfect for this.
See the tutorial for java projects.
Create a build.gradle file, where you use the java plugin:
apply plugin: 'java'
You either need to move your java files to src/main/java, or specify the source folder in build.gradle, something like this:
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
Then run 'gradle jar'