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'
Related
I have a Java package that I am building with Gradle. The structure of my package is like so:
PackageName
configuration/
f1/
config.file
src/
main/
java/
com.my.company.tests/
....test files.java
com.my.company.testconfiguration/
....test configuration.xml
logconfiguration.xml
someJson.json
someJson2.json
My gradle file has the following lines:
apply plugin: 'my-company-plugin'
sourceSets.main.java.srcDirs = ['src']
sourceSets.main.resources.srcDirs = ['configuration']
sourceSets.main.resources.includes = ['**/*.xml']
sourceSets.main.resources.includes = ['**/*.json']
task copyConfiguration(type: Copy) {
from "${project.projectDir}/configuration"
into company.buildDir
}
build.dependsOn copyConfiguration
When I build and generate a JAR from this, i am trying to verify that all the XML files as well as the .json files are present. But my JAR is not showing any of them. When I run jar tf mypackage.jar it shows
com/
com/my/
com/my/company/
com/my/company/tests/
com/my/company/tests/TestClass1.class
com/my/company/tests/TestClass2.class
f1/
Am I misreading something or what did I misconfigure? I am very new to gradle and I don't really understand sourceSets so would appreciate if someone could provide guidance.
I am on mac so jar is the only way I know how to look at the contents of a JAR. I don't believe 7zip is available for mac.
How can I exclude a source folder from a jar file using Gradle Kotlin DSL?
The source sets are
sourceSets {
main {
java {
srcDirs("src/main/java")
srcDirs("src/util/java")
}
}
}
and I want to exclude src/util/java from Jars.
I tried
tasks {
withType<Jar> {
exclude("src/util/java/**")
}
}
but that does ot work because the string likely does not contain the source folder.
What is the best way to achieve this?
Try to filter in the compile task.
tasks.withType(JavaCompile) {
exclude '**/util/**/*.java'
}
but this is appropriate if you looking to exclude some specific files.
If the whole src folder (like util) is to be excluded, it would be better redefine the sourceSets.main.java to only src folders you need - for that module
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.
I have a gradle based project with 8 subprojects. Each of these has test code that relies on a folder with a number of .csv files for content. I'd rather not copy this folder into the test path of each subproject.
Is there a way to tell gradle to look for the files in a common location?
How about for IntelliJ? I tried indicating the files were a 'test resource' but haven't figured out how to get each sub-module to find them (without resorting to '../..' style notation)
I did that providing the path to the common files folder on sourceSets in the build.gradle file:
sourceSets {
test {
resources {
srcDirs = ["src/test/resources", "src/main/resources/db"]
}
}
Not sure if that's a clean way to do that, but for me it's better than copypasting the same files over multiple locations.
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.