Using gradle 5.4 with a project dependency from external folder in a netbeans project.
The external folder contains resources like images, xml and custom objects that can only be created by this netbeans project. These external assets are then used to create binary files that get packed into a separate jar by that netbeans project.
These same external resources are also used during runtime for development in the gradle project. While I need the resources for development in the gradle project, I do not need or want them to be included in any jars anywhere for any reason when using the task build command because only the binaries are needed for distribution.
How to exclude the external resources from any and all jar files in the gradle project but allow them to be used for the classPath so I can run the project?
Some code examples of failure.
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
sourceSets {
main {
resources {
srcDir '../ExternalResourceFolder/assets'
}
}
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
dependencies {
runtimeOnly files('../ExternalResourceFolder/assets')
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
distributions {
main {
contents {
exclude '../ExternalResourceFolder/assets/**'
}
}
}
Tried many more things like adding to classPath and exclude but it would just be clutter to add them. Changing from sourceSet to dependency only moves the problem around from "build/lib" folder to "build/distributions" folder.
Had to exclude per file type in the end.
sourceSets {
main {
resources {
srcDir '.'
exclude ('**/*.j3odata','**/*.mesh','**/*.skeleton',\
'**/*.mesh.xml','**/*.skeleton.xml','**/*.scene',\
'**/*.material','**/*.obj','**/*.mtl','**/*.3ds',\
'**/*.dae','**/*.blend','**/*.blend*[0-9]','**/*.bin',\
'**/*.gltf')
}
}
}
Related
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'm attempting to include a generated pom.xml in the jar that I'm creating with gradle.
So far, in my parent project, I have
subprojects {
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
}
and in the sub-project I have:
tasks.build.dependsOn install
sourceSets {
main {
resources {
srcDirs = [ "src/main/resources", "build/poms" ]
}
}
}
This will generate ./build/poms/pom-default.xml, but it will not add it to the JAR.
Creating a dependency on an earlier phase than build creates circular dependencies (and I don't know whether this is the problem anyway).
Also, I'd like the pom.xml to show up inside META-INF with name pom.xml (not pom-default.xml), so this may not be the right approach anyway.
Somehow I'm thinking it can't be as complicated as this looks?
You should be able to include the POM in your JAR by adding the following to your subprojects closure:
jar {
into("META-INF/maven/${project.group}/${project.name}") {
from generatePomFileForMavenPublication
rename { it.replace('pom-default.xml', 'pom.xml') }
}
}
If you already have a jar closure, you can add it there. This automatically creates a task dependency on the generatePomFileForMavenPublication task, so that the POM file is there when the JAR is created.
The sourceSets part from your question would not be required for this.
(Side note: It would not be strictly necessary to do this at all, because the Maven publish process will publish the POM as an individual artifact anyway.)
I've got a basic Java application in which I would like to use WebJars. I use Gradle as my build system. I would like to use the WebJars for Bootstrap and JQuery so I can easily reference and update them in my Spring-Boot/ThymeLeaf application. The application is basically the one from the form tutorial located here
As I understand it Gradle should place all the files from the WebJars into the META-INF folder in my Jar file. If I understand everything correctly the Spring-Boot resource handler will then load resource from META-INF/ when I reference something in my html page that starts with /webjars/
Unfortunately this doesn't work (yet). Since I see in Tomcat's log output that the resource handler is correctly installed. I decided to check if the files are actually in my Jar file.
When I extract my Jar file the META-INF folder only has a file called MANIFEST.MF with some information about Spring Boot. There is a BootStrap-3.3.7.Jar and a JQuery-3.2-1.Jar file in BOOT-INF/lib but I don't think that is where they are supposed to end up. (Or am I wrong and is there error somewhere in the resource handler?).
How do I tell gradle to do the right thing with these files when I run gradle build?
My gradle.build file looks like this:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "jacoco"
jar {
baseName = 'gs-serving-web-content'
version = '0.0.1'
}
bootRun {
addResources = true
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.webjars:jquery:3.2.1")
compile("org.webjars:bootstrap:3.3.7")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Gradle is doing the right thing as the jars should be packaged in BOOT-INF/lib. The root of each jar in BOOT-INF/lib is then added to the classpath from where each its webjar related content in META-INF content should be found.
I'd recommend asking another question that focuses on what your application's doing at runtime. As far as I can tell, everything's working as it should at build time.
I have a frontend and a backend repository for my app. The backend is written in Go and serves the API over gRPC. The generated gRPC Java files end up in backend-repo/proto-gen/java/ (so backend-repo/proto-gen/java/com/myApp/Users.java).
In my frontend android repo I have submoduled the backend repo to a folder called server. I want to modify my build.gradle to compile the .java files from the backend.
android-repo/
app/
build.gradle
server/proto-gen/java/com/myApp/
Users.java
AnotherService.java
I'm very new to Android development and am struggling to figure out the right approach.
This is a snippet from my app/build.gradle but it fails because it can't find the package com.google.protobuf.
task compileGrpc (type: JavaCompile) {
source = fileTree(dir: '../server/proto-gen/java/', include: '**/*.java')
destinationDir = file('build/classes')
classpath = files('../server/proto-gen/java/')
options.compilerArgs = ["-sourcepath", "$projectDir/../server/proto-gen/java/"]
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0-alpha-2'
compileGrpc.execute()
}
You can add your dependencies like
dependencies {
classpath files('server/proto-gen/java/com/myApp/')
}
This will accept relative path.
Simply including the path to the src sourceset for the project would include them within the compilation:
android {
...
sourceSets {
main {
java.srcDirs = [java.srcDirs, 'server/proto-gen/java']
}
}
}
You'll be able to see the src files/directories included within the Android View/Project Explorer on the right side and edit the files directly.
I have the following project:
-> root
->->common
->->server
->->client
I want the server and client projects to both access files from the same resource folder.
My root's build.gradle looks like the following:
apply plugin: 'java'
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
}
subprojects {
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.8.1'
}
}
The multi-project system works and I have no issues with it. I have found resources for implementing resource folders but they're only at a per-project level.
I'd appreciate any insight and help in this. :)
If you have resources you want shared across projects you should create a library for those resources, and have each project depend on that library. Take a look at the Android documentation on library modules.
Also, take a look here for an example of how to add a project dependency to your Gradle build script.
In case you prefer not to have another artifact then another option would be to merge the client/server code with the common code before building (e.g. $buildDir/src) and pointing the srcDir to this location.
In order to do this:
Add a prepareSources task of type Copy that will copy all relevant sources both from client/server and common into alternative src/main folder (e.g. under $project.buildDir/src)
Rename the src/main folder in the server/client modules to something else. This is needed as the java plugin automatically includes this folder in srcDir while we want to take these sources from the merged sources location.
Make compileJava and processResources dependent on the new prepareSources task:
compileJava.dependsOn prepareSources
processResources.dependsOn prepareSources
Add the new src directory to srcDir