I like to include some build variables in my application. For example a buildnumber. Can I pass some variables from a ant build script to my Android application and use that variable in java?
Anybody has a link or example?
Use an ant task (such as replaceregexp) to insert the build numbers and other variables into a class for that purpose.
<replaceregexp file="${my.version.class.file}" match="#version#" replace="#${build.number})#" />
Or something similar. Basically you want to compile the information into a class as part of the build process.
Just populate the build number into a string xml file and read it as normal. You can use the replace task to do that..
Dont use a properties file since there is not Android native way to read it. The native way are string values in the xml files. And it is better to replace it into a static strings xml file rather than java source code too.
One thing you can do is to first use the "echo" task to create a properties file. That task supports variable substitution, so you can include all build variables. Then include that properties file in your application jar and use the Java Properties to read them.
See:
http://ant.apache.org/manual/Tasks/echo.html
http://download.oracle.com/javase/6/docs/api/java/util/Properties.html
Related
The .cfg file created by JPackage contains some useful metadata such as the app.version. Here's a snippet...
app.name=MyApp
app.version=1.0
app.runtime=$APPDIR\runtime
app.identifier=com.company.package
app.classpath=
app.mainmodule=application/com.company.package.Main
[JavaOptions]
[ArgOptions]
Is there a simple way to query this file through Java without manually loading the file from the file system and parsing it?
For context, we would have liked to retrieve the version from the manifest within the JAR, but unfortunately this doesn't seem to be available when the application is built using modules.
Cheers.
Some of those fields can be read from the system properties inside the app - assuming you use jpackage --app-version flag:
System.getProperty("jpackage.app-path");
System.getProperty("jpackage.app-version");
System.getProperty("java.home");
System.getProperty("java.class.path");
If there are other fields that are important to you it is also possible to specify other settings to apply to launchers using --java-options "-Dyour.field=XYZ"
I have to pass database, username and password to connect to a database from VSTS. I am executing a maven project.
I defined the variables in VSTS.
I want to have a property file like
datasource.url="${database}"
datasource.username="${username}"
datasource.password="${password}"
Is possible to do this? or should I get the variables from the java code?
Is possible to do this? or should I get the variables from the java
code?
Of course it can. And you don't need to do other complex operation, just use an task which name is Replace Tokens task.
It's not only work in the .config file, but also very effective in the .properties file.
As your sample, just change the definition of the variable so that it can be access correctly:
datasource.url="#{database}#"
datasource.username="#{username}#"
datasource.password="#{password}#"
The format of variable in .properties file is #{VariableName}#.
And then, add the task Replace Tokens task into your pipeline. Add the value on the Variable tab:
For task configuration, just need to specify the value of Target files:
After build filnished, locate to the local file. Here is my test result, you can see that the replace is successful:
Note: While you build with this task, it will report some error message like: ##[error]EISDIR: illegal operation on a directory, read. But actually it does not affect these variable replace operation. You will see that the file has been replace successfully.
Create a task for it. For example in build pipeline:
- task: Bash#3
inputs:
targetType: inline
script: 'sed -i "s/\${database}/\$(database)/g" db.properties'
displayName: 'inject properties'
And so on.
I'm going to have a lot of submodules in my main project directory x, like x/module1, x/module2...
can i avoid manually adding every single module into settings.gradle? can i somehow script it to find all the subdirectories and add them automatically?
As cricket_007 already mentioned, Gradle is based on the Groovy programming language (which is, like Java, executed in the JVM) and the settings.gradle file is nothing more but a Groovy script.
Whenever you use include 'project', the include method of a Settings instance is called, so for your goal, you could simply create a loop which iterates over all folders and calls include for each of them.
A more 'groovyesque' approach would be the usage of a closure for each subdirectory, provided by the Groovy SDK extension for the File class:
file('.').eachDir { sub ->
include sub.name
}
There are multiple ways to solve your problem, e.g. since the include method accepts an array of project path strings, you could also aggregate all required paths first and pass them all together. Simply get familiar with the Gradle docs and decide on your own, what solution suits your case the best.
I have an Android application that is by default built with Ant.
There is build.xml file which loads local.proerties file. I would like to add my custom property (for example Google Maps apiKey) and access it in Java classes - for example in some MainActivity.
How can I achieve it ?
Make your ant script modify the Java source file containing the API key or a properties file bundled with the application, using the replace task or the copy task with a filterset.
Ant provides a target called "sysproperty". Its like a property tag, but it sets the java System.property. So, you can do something like:
<sysproperty name="foo" value="$THE_EXTRA_PROPERTY">
where "THE_EXTRA_PROPERTY" is the extra property that you added to the properties file that gets loaded.
I have my java code running.
I added a groovy shell to evaluate the main groovy file.
What it does is simple.
GroovyShell.run (main.groovy)
Now, in my main.groovy, if I have other .groovy files I'd like to "require", how can I do that?
Is there something like "require, source, load, require_one, import" filename?
http://groovy.codehaus.org/Embedding+Groovy
if you scroll down to the section entitled "Dynamically loading and running Groovy code inside Java" you will see a full example with two different approaches to solving your problem
Actually, here is the answer:
I couldn't find a function like that in Groovy.
So, when evaluating your file, you have to parse all groovy files.
If you add new files on the fly and would like to evaluate them, you can't reuse the shell, not even through a singleton via the evaluated script
What you can do is register your parsed files and force the shell to "re-parse" them all again when re-evaluating your file.