Trying to add Applied Energistics api through build.gradle - gradle

I am starting on a minecraft mod, but I cannot get the build.gradle right.
I wanted to create a mod on top of applied energistics but I cannot get it to build when I add this mod as a gradle dependency. I looked all the dependencies up on an other repository, but when I build it gives errors like below, and some other errors on the general minecraft code base
Error:(3, 26) java: package net.minecraft.init does not exist
This is the code I have right now:
buildscript {
repositories {
jcenter()
maven { url = "http://files.minecraftforge.net/maven" }
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
version = mod_version + "-" + mod_channel
group = mod_group
archivesBaseName = mod_basename
sourceCompatibility = targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
compileJava {
sourceCompatibility = targetCompatibility = '1.8'
}
minecraft {
version = minecraft_version + "-" + forge_version
replaceIn "package-info.java"
replace "#version#", project.version
replace "#modversion#", mod_version
replace "#modchannel#", mod_channel
// used when launching minecraft in dev env
mappings = mcp_mappings
}
repositories {
maven {
name 'Mobius Repo'
url "http://mobiusstrip.eu/maven"
}
maven {
name = "JEI repo"
url "http://dvs1.progwml6.com/files/maven"
}
}
dependencies {
// installable runtime dependencies
compileOnly "mcp.mobius.waila:Hwyla:${hwyla_version}"
// compile against provided APIs
compileOnly "mezz.jei:jei_${minecraft_version}:${jei_version}:api"
compileOnly "mcp.mobius.waila:Hwyla:${hwyla_version}"
// at runtime, use the full JEI jar
runtime "mezz.jei:jei_${minecraft_version}:${jei_version}"
deobfCompile "appeng:appliedenergistics2:${ae_verion}"
}
processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else except the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
The version that I am trying to add is:
ae_verion=rv5-stable-8

You need to add 'api' at the end of the compile argument (used to be 'dev'). Also, iirc adding transitive=false solves dependency issues.
dependencies {
compile ("appeng:appliedenergistics2:${ae_version}:api") {
transitive = false
}
}

Related

I got an error using this build.grafle file and don't know how to fix it

Here's the Error:
FAILURE: Build failed with an exception.
Where: Build file '/home/wieland/GitGradlePackaging/build.gradle' line: 22
What went wrong: A problem occurred evaluating root project 'GitGradlePackaging'.
Could not get unknown property 'org' for object of type org.gradle.api.internal.initialization.DefaultScriptHandler.
And Here's my build.gradle File:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html
*/
//From example: http://mrhaki.blogspot.co.at/2015/04/gradle-goodness-use-git-commit-id-in.html
buildscript {
repositories {
jcenter()
}
dependencies {
//Add dependencies for build script, so we can access Git from our build script
classpath 'org.ajoberstar:grgit:1.1.0'
}
def git = org.ajoberstar.grgit.Grgit.open(file('.'))
//To save Githash
def githash = git.head().abbreviatedId
}
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building an application
id 'application'
// Apply the groovy plugin to also add support for Groovy (needed for Spock)
id 'groovy'
id 'distribution'
}
// Set version
project.version = mainProjectVersion + " - " + githash
project.ext.set("wholeVersion", "$project.version - $githash")
project.ext.set("buildtimestamp", "2000-01-01 00:00")
def versionfilename = "versioninfo.txt"
def GROUP_DEBUG = 'Debug'
// Task to print project infos
task debugInitialSettings {
group = GROUP_DEBUG
doLast {
println 'Version: ' + project.wholeVersion
println 'Timestamp: ' + project.buildtimestamp
println 'Filename: ' + project.name
}
}
// To add the githash to zip
task renameZip {
doLast {
new File ("$buildDir/distributions/$project.name-${project.version}.zip")
.renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.zip")
}
}
distZip.finalizedBy renameZip
// To add the githash to tar
task renameTar{
doLast {
new File ("$buildDir/distributions/$project.name-${project.version}.tar")
.renameTo ("$buildDir/distributions/$project.name-${project.wholeVersion}.tar")
}
}
distTar.finalizedBy renameTar
// Define the main class for the application
mainClassName = 'App'
dependencies {
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:23.0'
// Use the latest Groovy version for Spock testing
testCompile 'org.codehaus.groovy:groovy-all:2.4.13'
// Use the awesome Spock testing and specification framework even with Java
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testCompile 'junit:junit:4.12'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
//To generate Testreports as HTML
test {
reports {
junitXml.enabled = false
html.enabled = true
}
}
distributions {
main {
contents {
from { 'build/docs' }
into ('reports') {
from 'build/reports'
}
}
}
}
//To make sure that test and javadoc ran before zip and tar
distTar.dependsOn test
distZip.dependsOn test
distTar.dependsOn javadoc
distZip.dependsOn javadoc
Please keep in mind I have not much knowledge about gradle as I'm just starting to learn it!
Thanks in advance :)
You have to move the githash definition outside the buildscript block
buildscript {
repositories {
jcenter()
}
dependencies {
//Add dependencies for build script, so we can access Git from our build script
classpath 'org.ajoberstar:grgit:1.1.0'
}
}
def git = org.ajoberstar.grgit.Grgit.open(file('.'))
//To save Githash
def githash = git.head().abbreviatedId
The reason is that when the buildscript block is evaluated line by line, its dependencies are not yet loaded. When the rest of the script is evaluated, the dependencies of the buildscript block have already been loaded. This is actually the reason for the buildscript block existence: to be run before the rest of the build and prepare the setup.

How to build Google protocol buffers and Kotlin using Gradle?

I'm trying to build a project that uses both Google protocol buffers and Kotlin using Gradle. I want the proto files to compile into Java source, which is then called from my Kotlin code.
My source files are arranged like this:
src/main/proto/*.proto
src/main/kotlin/*.kt
src/test/kotlin/*.kt
Here's my build.gradle file:
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
buildscript {
ext.kotlin_version = '1.1-M02'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.12'
}
When I run ./gradlew assemble I get a bunch of "Unresolved reference" errors during :compileKotlin. Afterwards I can see that there are no Java source files generated, so it appears that the proto compiler is not being invoked at all.
If I remove the apply plugin: 'kotlin' line, then ./gradlew assemble successfully generates the Java source, but of course my Kotlin source is never compiled.
How do I fix my build.gradle so that I can call my protobuf code from Kotlin?
To get protobuf-gradle-plugin and kotlin-gradle-plugin to cooperate, you need to ensure that the Java code is (re)generated before invoking the Kotlin compiler.
For Gradle's default source sets, main and test, you can do that like this:
compileKotlin.dependsOn ':generateProto'
compileTestKotlin.dependsOn ':generateTestProto'
If you are using other source sets, you'll need to make adjustments.
Older versions of protobuf-gradle-plugin also required updating sourceSets, but newer versions do not seem to require this.
// Don't do this with protobuf-gradle-plugin 0.9.0 or higher
sourceSets.main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/java"
sourceSets.test.java.srcDirs += "${protobuf.generatedFilesBaseDir}/test/java"
For Kotlin and Android:
android {
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/debug/java'
release.java.srcDirs += 'build/generated/source/proto/release/java'
}
}
An additional source directory has to be added for every build type. In this sample there are two build types: debug and release.
If you're using grpc, another line has to be added per build type:
android {
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/debug/java'
debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
release.java.srcDirs += 'build/generated/source/proto/release/java'
release.java.srcDirs += 'build/generated/source/proto/release/grpc'
}
}
At least with Kotlin 1.0.6, protobuf-gradle-plugin 0.8.0, protobuf 3.2.x and grpc 1.x it's not required to fiddle with the task order.
if you are working with multiple build types and flavors in android and with protobuf-lite use below with kotlin.
for example I have debug and release builds with demo and prod flavors it will create demoDebug, demoRelease and prodDebug and prodRelease variants.
then use
`
android{
sourceSets {
debug.java.srcDirs += 'build/generated/source/proto/demoDebug/javalite'
debug.java.srcDirs += 'build/generated/source/proto/prodDebug/javalite'
release.java.srcDirs += 'build/generated/source/proto/demoRelease/javalite'
release.java.srcDirs += 'build/generated/source/proto/prodRelease/javalite'
}
}
`
tie the different compileKotlin with generateProto
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
if (getName() == 'compileDemoDebugKotlin')
dependsOn(':app:generateDemoDebugProto')
if (getName() == 'compileDemoReleaseKotlin')
dependsOn(':app:generateDemoReleaseProto')
if (getName() == 'compileProdDebugKotlin')
dependsOn(':app:generateProdDebugProto')
if (getName() == 'compileProdReleaseKotlin')
dependsOn(':app:generateProdReleaseProto')
}
For the gradle setup :
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.protobuf' version "0.8.17"
}
Then at the bottom of the build.gradle
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
// Generates the java Protobuf-lite code for the Protobufs in this project. See
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
// for more information.
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}

Building a fully executable Spring Boot 1.3 war from a Gradle multi project build

I'm trying to build a fully executable WAR using Spring Boot 1.3 as per https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html. If I build a single Gradle project, it all works fine, but I havea multi project build, where I have a "root" project and then several projects underneath it, and I cannot get it to build anything but a standard, "fat" WAR file, without the providedRuntime of Jetty and without the scripts to make it run.
Does anyone know how to do this?
In my root project, I have the following (abridged):
buildscript {
repositories {
mavenCentral()
}
ext {
springBootVersion = '1.3.0.RELEASE'
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
allprojects {
//Put instructions for all projects
repositories {
mavenCentral() // jcenter is missing spring-orm.4.1.6.RELEASE jar file so try mavenCentral first
jcenter {
url "http://jcenter.bintray.com/"
}
maven { url 'http://repo.opensourceagility.com/release' }
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'spring-boot'
}
and then in the subproject which is a web project, and which I'm trying to build, I have:
apply plugin: 'war'
dependencies {
// Include related projects
compile project(':project-model')
compile project(':project-dynamoDB')
// Core Spring Boot - note version is set in main build.gradle file
compile 'org.springframework.boot:spring-boot-starter-web'
// Remove Tomcat (included in -web) and include Jetty instead
providedRuntime 'org.springframework.boot:spring-boot-starter-jetty'
// Other Spring modules
compile 'org.springframework.boot:spring-boot-starter-social-facebook'
compile 'org.springframework.boot:spring-boot-starter-social-linkedin'
compile 'org.springframework.social:spring-social-google:1.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-devtools'
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-context-support'
}
configurations {
providedRuntime.exclude group: 'org.springframework.boot', module:'spring-boot-starter-tomcat'
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' // exclude when using log4j
}
springBoot {
mainClass = 'rs.web.Weblication'
executable = true
}
bootRun {
addResources = true
}
processResources {
// exclude resources if they look like they're profile dependent but don't match the current env/profile
eachFile { d ->
if(d.name.endsWith('.xml') || d.name.endsWith('.yaml') || d.name.endsWith('.properties')) {
//def fname = d.name.replaceFirst(~/\.[^\.]+$/, '')
//if(fname.indexOf("-") > -1 && ! fname.endsWith("-" + environment)) {
// d.exclude()
//} else {
// replace #variables# listed below in properties/config files
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
activeProfiles: environment
])
//}
}
}
}
war {
baseName = 'project-web'
version = '1.0.0'
manifest {
attributes 'Implementation-Title': baseName,
'Implementation-Version': version
}
webXml = file('src/main/resources/web.xml')
// rename the war task which has profiles appended from warName-profile,profile2.war
// to warName-profile.profile2.war
classifier = environment.replaceAll(',','-')
}
but when I build it (./gradlew build, or ./gradlew subprojectname:build), all is well and a working WAR is created, but not an executable one.
With a single project, I have it working fine.
Ah ha, right well I build a test multi-project build and it worked OK, so it was clearly the configuration above.
I worked through a process of elimination and it turns out that the problematic area was the line
classifier = environment.replaceAll(',','-')
which is intended to rename files with environment variables as part of the name. This process seems to get in the way of the script addition; perhaps it could be applied afterwards if it's really necessary.

Upload only war/jar files in gradle(restrict zip/tar generation and upload)

My build script is like as follows. I use gradle build command to build and gradle upload command to upload the artifact. My problem is a tar,zip file is also generated with this command and get uploaded. I dont want it. Only things I would like to get uploaded is 'jar' and 'war' files.
I have also a related question posted by me yesterday here.
More details(I have excluded some unwanted code)
build file in root
allprojects {
apply plugin: 'maven'
group = 'groupid'
version = '1.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusUrl"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "$nexusSnapshotUrl") {
authentication(userName: "$nexusUser", password: "$nexusPass")
}
}
}
}
}
ext.comlib = [ // Groovy map literal
junit3: "junit:junit:3.8",
junit4: "junit:junit:4.9",
spring_core: "org.springframework:spring-core:3.1",
hibernate_validator : "org.hibernate:hibernate-validator:5.1.3.Final",
spring_core : "org.springframework.security:spring-security-core:4.0.2.RELEASE",
spring_security_web: "org.springframework.security:spring-security-web:4.0.2.RELEASE",
spring_security_config: "org.springframework.security:spring-security-config:4.0.2.RELEASE",
spring_boot_starter_test: "org.springframework.boot:spring-boot-starter-test:1.2.5.RELEASE",
spring_boot_starter_actuator: "org.springframework.boot:spring-boot-starter-actuator:1.2.5.RELEASE",
spring_boot_plugin_gradle: "org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE",
asciidoctor_gradle_plugin: "org.asciidoctor:asciidoctor-gradle-plugin:1.5.1",
asciidoctor_pdf_plugin: "org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.9"/*,
sl4j_api: "org.slf4j:slf4j-api:1.7.12",
sl4j_log4j: "org.slf4j:slf4j-log4j12:1.7.12",
logback_classic: "ch.qos.logback:logback-classic:1.1.3",
logback_core: "ch.qos.logback:logback-core:1.1.3"*/
]
build file in sub module
apply plugin: 'spring-boot'
group = 'com.group.id'
apply from: "../build.gradle"
apply plugin: 'org.asciidoctor.gradle.asciidoctor'
apply plugin: 'war'
description = 'module name'
dependencies {
compile "someothermodule:commonapi:1.0.0-SNAPSHOT"
compile "io.springfox:springfox-swagger2:2.0.1"
compile project(':dependingproject1:dependingproject2')
compile comlib.spring_boot_starter_actuator
compile comlib.spring_core
compile comlib.spring_security_web
compile comlib.spring_security_config
testCompile(comlib.spring_boot_starter_test) {
exclude(module: 'commons-logging')
}
testCompile comlib.junit4
providedCompile comlib_app.spring_boot_plugin_tomcat
testCompile "io.springfox:springfox-staticdocs:2.0.3"
testCompile "org.springframework:spring-test:4.1.7.RELEASE"
}
ext {
swaggerOutputDir = file("src/docs/asciidoc/generated")
asciiDocOutputDir = file("${buildDir}/asciidoc")
}
test {
systemProperty 'org.springframework.restdocs.outputDir', asciiDocOutputDir
systemProperty 'io.springfox.staticdocs.outputDir', swaggerOutputDir
}
//spring boot plugin
buildscript {
repositories {
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusCentral"
}
maven {
credentials {
username "$nexusUser"
password "$nexusPass"
}
url "$nexusThirdParty"
}
}
dependencies {
classpath(comlib.spring_boot_plugin_gradle)
}
}
Included the following code snippet in my gradle file
[distZip, distTar].each { task -> configurations.archives.artifacts.removeAll
{ it.class.simpleName == "ArchivePublishArtifact" && it.archiveTask == task }
task.enabled = false
}
For more details refer this link. Its issue with spring boot plugin.
arjuncc's solution doesn't seem to work on Gradle 4.10.2, so here's one that works and uses public APIs, hopefully it will continue to work.
configurations.archives.artifacts.removeAll {
// exclude from the archives configuration all artifacts that were generated by distZip & distTar
def depTasks = it.getBuildDependencies().getDependencies()
depTasks.contains(distZip) || depTasks.contains(distTar)
}
More or less the same as ajuncc's solution, but perhaps a bit more simple. Remove all .tar artifacts from the archives configuration:
configurations.archives.artifacts.removeAll {PublishArtifact publishArtifact -> publishArtifact.type == 'tar'}

Adding a 'provided' configuration to a kotlin/java library using gradle

I have a java library to which I've added a 'provided' configuration using gradle.
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'checkstyle'
apply from: file('../gradle/gradle-mvn-push.gradle')
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
group = GROUP
version = VERSION_NAME
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
javadoc.classpath += configurations.provided
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
checkstyle {
configFile = new File(rootDir, 'checkstyle.xml')
toolVersion = '6.7'
}
dependencies {
provided 'com.google.android:android:2.3.1'
compile 'com.squareup:javawriter:2.5.0'
compile 'com.workday:metajava:1.0'
}
Everything works as expected with this setup.
Now I want to start using kotlin in my library, so I modified the build script to look like this (sans pluses)
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.613"
+ }
+}
apply plugin: 'java'
apply plugin: 'idea'
+apply plugin: 'kotlin'
apply plugin: 'checkstyle'
apply from: file('../gradle/gradle-mvn-push.gradle')
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
group = GROUP
version = VERSION_NAME
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
javadoc.classpath += configurations.provided
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
checkstyle {
configFile = new File(rootDir, 'checkstyle.xml')
toolVersion = '6.7'
}
dependencies {
provided 'com.google.android:android:2.3.1'
compile 'com.squareup:javawriter:2.5.0'
compile 'com.workday:metajava:1.0'
+ compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.613"
}
Without even adding any kotlin files (and even if I do), the library no longer compiles saying that it can't find the classes in the provided library (android in my case).
What do I need to change to get it so that the provided dependencies are recognized again?
That's how I do it in a Gradle plugin:
private void addProvidedConfiguration() {
final Configuration provided =
addConfiguration(project.configurations, "provided")
final Javadoc javadoc =
project.tasks.getByName(JavaPlugin.JAVADOC_TASK_NAME) as Javadoc
javadoc.classpath = javadoc.classpath.plus(provided)
}
private static Configuration addConfiguration(
final ConfigurationContainer configurations, final String name) {
final Configuration compile =
configurations.getByName(JavaPlugin.COMPILE_CONFIGURATION_NAME)
final Configuration configuration = configurations.create(name)
compile.extendsFrom(configuration)
configuration.visible = false
configuration.transitive = false
configuration.allDependencies.all { final dependency ->
configurations.default.exclude(group: dependency.group, module: dependency.name)
}
return configuration
}
You can check the source code on github.
This gradle-propdeps plugin for Gradle adds provided and optional to Gradle, and also handles its integration into IntelliJ IDEA and Eclipse. It is from the Spring team and it has a lot of usage.
You should apply the kotlin plugin after propdeps.
I have not tested this with Android libraries, but it is a much safer bet than trying to roll your own. There are little special cases to handle.
An alternative plugin, also used heavily is the one from Netflix team who has many plugins available for different useful additions to Gradle. This plugin adds the same provided and optional.
This is a good reminder that the old issue Provide a 'provided' configuration for Gradle is still outstanding for 6 years, and feel free to provide your opinion there as to the lack of this feature.

Resources