This message was deleted.
# community-support
s
This message was deleted.
🧵 1
v
Please do not split topics across several threads. This makes following the conversation in the threads view very hard as the context of the other messages is missing. If you have additional information either edit the original message or post to its thread. Regarding the question, having the same input and output is a very bad idea and most probably the source of your problems. Also, not using a copy task but modifying the files in-place would also not be any better. I recommend to instead have some resource file with those placeholders and then just configure the placeholder expansion on the
processResources
task without creating a new task and then read from that resource file using a Java file.
r
plugins { // id "com.github.spotbugs" version "4.5.1" id "checkstyle" } apply plugin: 'application' apply plugin: 'jacoco' mainClassName = 'main/java/memoranda/Start' // Repositories and dependencies for code go here repositories { mavenCentral() jcenter() } dependencies { implementation "junitjunit4.12" implementation 'com.io7m.xomxom1.2.+' implementation 'net.sourceforge.nekohtmlnekohtml1.9.+' implementation 'xomxom1.3.9+' implementation 'org.hamcresthamcrest core1.3+' implementation 'xalanxalan2.7.0+' implementation 'xercesxercesImpl2.11.0+' } /////////////////////////////////////////////////////////////////////// // Configure Jacoco /////////////////////////////////////////////////////////////////////// jacocoTestReport { dependsOn test // tests are required to run before generating the report } jacoco { toolVersion = "0.8.8" reportsDirectory = layout.buildDirectory.dir('customJacocoReportDir') } jacocoTestReport { reports { xml.required = false csv.required = false html.outputLocation = layout.buildDirectory.dir('jacocoHtml') } } /////////////////////////////////////////////////////////////////////// // Configure Checkstyle ///////////////////////////////////////////////////////////////////// apply plugin: 'checkstyle' // checkstyle { // // Keep checkstyle a little quieter // ignoreFailures = true // showViolations = false // } tasks.withType(Checkstyle) { reports { html.enabled = true xml.enabled = false } // include '**/*.java' include '**/ui/App.java' } checkstyle { toolVersion = "8.8" } dependencies { implementation "junitjunit4.12" } /////////////////////////////////////////////////////////////////////// // Configure SpotBugs /////////////////////////////////////////////////////////////////////// // To generate an HTML report instead of XML // spotbugsMain { // reports { // xml.enabled = false // html.enabled = true // } // } // spotbugs { // toolVersion = '4.2.2' // ignoreFailures = true // showProgress = true // effort = 'max' // }
This is the build.gradle file but I am trying to set the two version and build variables and I have tried different methods
Copy code
public static final String VERSION_INFO = "@VERSION@";
public static final String BUILD_INFO = "@BUILD@";
These are the variables but I cant understand how to implement or call the java file in the gradle to set the variables
I used this in there too thinking it would actually set the variables in the Java file
Copy code
task processAppJava(type: Copy) {
    from 'src/main/java/memoranda/ui'
    include 'App.java'
    into 'build/modified-java'

    // define the variables
    def VERSION_INFO = '@VERSION@'
    def NEW_VERSION = '4.5.1'
    def BUILD_INFO = '@BUILD@'
    def NEW_BUILD = '8.1'

    filter { line ->
        line.replace(VERSION_INFO, NEW_VERSION)
                .replace(BUILD_INFO, NEW_BUILD)
    }

    doLast {
        println version
    }

}
v
It will set it within
build/modified-java
, yes. But from there it will not end up in any compilation or result.
And I'm not sure why you posted that big pile of bad practices. Should there be something relevant to this discussion I don't see?
a
it looks like token substitution does part of what you're asking https://docs.gradle.org/8.6/userguide/working_with_files.html#sec:filtering_files Your new Copy task will update the Java code and place it into
build/modified-java
, but have you set up that directory as a source dir?
v
Would be a bad idea if he did
because the class is then in the actual source dir and in the generated source dir and you can throw dices which will end up in the end result
There is nothing intended to replace something in the java files before compiling them. For resources files there is, which is why I recommended to use a properties file with those placeholders or similar and from the code read that resource file
That's the typical pattern for this.
a
There's also a Gradle plugin for generating a 'build constants' file - I see there's an example in the README for using the Groovy DSL (i.e. in a
build.gradle
file) that will generate a Java file with build constants https://github.com/gmazzo/gradle-buildconfig-plugin
r
Okay then how do you output the variables I have set to a string?
Is what I am putting down actually close to being able to print out correctly or at least run appropriately?
a
sorry, I don't quite follow :) Do I understand correctly that you want to access the version of your project in your Java source code?
v
Assuming the answer to Adams question is yes, I already said two times how to do it. Have some resource file like a
...properties
file in
src/main/resources
, in it have your placeholders, then configure the
processResources
task with your filter / replace / expand configuration, and at runtime read the values from this resource file.
r
Okay I am sorry let me find a better way to explain
I want the Update Version and Build Number to be correct to always see the correct version and build information in my Memoranda application
v
And what do you not like with the approach everyone else is using and that I described three times already?
a
gotcha, that makes sense. And do you want to implement the task yourself? Which of course is a fun learning experience, but otherwise I really would suggest using https://github.com/gmazzo/gradle-buildconfig-plugin since it should Just Work™️
r
Oh I just don't understand the properties, I am sorry I just don't understand how to implement the properties I thought I might have tried to do so before but my teacher told me that it is much easier
v
You don't understand what with properties?
They are just a text file, and there are built-in classes that read it and give you key/value access to the contents
r
Oh okay I can try that I just don't fully know what I am doing wrong for the code not to implement correctly because I keep getting errors when i do the command gradle build
v
Which?
r
I don't undestand how to implement them in my build.gradle file, I think I am missing something because I did follow a website and it still gave me Build Fail
Both
v
Both what?
I asked which errors
To start with, you should probably not use an ancient Gradle version
r
I am opened minded to suggestions because this has been blocking me from moving further in my code and I dont think it should be this hard, well from my understanding it shouldn't be hard
Oh my bad, I misunderstood
v
And then I strongly recommend you switch to Kotlin DSL. It by now is the default, you immediately get typesafe build scripts, actually helpful error messages if you mess up the syntax, and an amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.
r
* What went wrong: Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details.
v
That's not a Gradle error at all
You just wrote bad Java code
Scroll up and read the compiler errors
r
Okay I am using INtelliJ IDEA
v
Then make sure you don't fell into the focus trap and select a higher node in the tree left to that output
r
There are a lot of errors but I was confused because I hadn't changed anything except in the build.gradle file and App.java
They show other files I haven't touched, I was confused on what I did there
Another screenshot
v
Well, there are 61 compile errors and we can hardly guess what you did to cause them
r
Okay, I should just start all over then
v
Quite possible
r
Okay I understand, I will do that and follow the suggestion above to help me out Thank you Vampire and Adam for your help.
👌 1