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.
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 a Spring Java 1.8 project exported in a .jar. When I want to run this Java .jar application, I'd like to give a file into the argument which .xml config file should the program use in the relative folder. I tried so many ways, but none of them works.
For example:
sampleJavaPorgram.jar config1.xml
(config1.xml and sampleJavaProgram.jar is in the same directory)
shoud load config1.xml like that:
ApplicationContext context = new ClassPathXmlApplicationContext("config1.xml");
Maybe use Command-Line Argument, to get name of this XML file and then pass it to AppContext.
Or you might use System properties
Once the build.xml file is executed, I want to check some properties in the build.xml to handle some errors in my Java code.
For infos, my Ant file (build.xml) is executed this way (not all the code is present):
ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null,
ITaskLauncherConstants.LAUNCHER_NAME);
/* Configure the ILaunchConfiguration (not all setAttribute calls are presents) :*/
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, "build.xml");
/* Launch the task, where monitor is a IProgressMonitor */
ILaunch lc = workingCopy.launch(ILaunchManager.RUN_MODE, monitor);
And in my build.xml file I have some properties :
<!-- In Ant File -->
<property name="fail.message" value="true" />
Is it possible to check the content of the property in Java code ? Something like :
/* In Java code */
lc.getProperty("fail.message");
Of course, this would mean that the class would "record" the build.xml properties.
Same question has been asked 6 months ago, but the answer is not working (I tried):
How to set a property in java code using build.xml
Thanks for any suggestion.
A simple approach would be using echoproperties task in your ant buildscript, means writing all or desired (via echoproperties attribute prefix) properties to a file and load that file in java afterwards.
From ant manual echoproperties, attribute destfile :
If specified, the value indicates the name of the file to send the
output of the statement to. The generated output file is compatible
for loading by any Java application as a property file. If not
specified, then the output will go to the Apache Ant log.
<echoproperties prefix="fail" destfile="what/ever/foo.properties"/>
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
Currently i have placed the Displaytag.properties in 'src' directory, and it is working fine. Is it posssible to have this file on some different location like src/comp/bre/sub/config ?
From the docs for the DisplayTag library:
For the whole web application, create
a custom properties file named
"displaytag.properties" and place it
in the application classpath.
Displaytag will use the locale of the
request object to determine the locale
of the property file to use; if the
key required does not exist in the
specified file, the key will be loaded
from a more general property file.
So in your case make sure your build scripts (or IDE) copy your displaytag.properties file from src/comp/bre/sub/config the onto the classpath.
In an IDE this is normally as simple as specifying that a particular directory contains source code. In ANT just make sure the displaytag.properties file ends up in your /WEB-INF/classes.
Find the code which loads this properties file and add the new path. You can also place it in a folder yourProject/src/resources/ and add it to the classpath. Therefore, your properties file will be placed in the binary folder once the code will be compiled.