0
votes

I have the following problem creating a .jar file using Ant.

I have a project into IntelliJ (but the situation would be the same with any IDE).

This project use some .properties files that are putted inside the project itself. So I have the following structure (related only to the .properties files).

PROJECT-NAME
     |
     |
     |-------> src
     |          |
     |          |
     |          |-------> config (it is a package)
     |          |           |
     |          |           |
     |          |           |-------> mailer.properties
     |          | 
     |          | 
     |          |-------> log4j.properties 
     |     
     |
     |-------> config.properties

So, as you can see from the previouse schema, I have 3 properties file into 3 different folders of the project:

  1. config.properties: this is into the project root folder.

  2. log4j.properties: this is into the /src/ folder of the project.

  3. mailer.properties: this is into the config package inside the /src/ folder of the project.

At this stage I have created a build.xml Ant script that don't create the config package and that don't put all my properties file in the correct locations.

So my doubt is: how to handle this kind of situation?

I am thinking that I can create the config folder into the compile target and copy the properties file in the correct location before create the final jar file using the jar target.

Is it a good solution? How can I handle this kind of situation?

1
what is your actual question? is it how to add the property file into the ant build xml?Nomesh DeSilva
@NomeshDeSilva yes, something like this. How to correctly add these file (because they have specific location and I think that also when the Ant script put these file into the .jar file these file have to be in the same location. or not?AndreaNobili
have you tried the PropertyTask in ant? I think you need to have it in the same level. please have a look at ant.apache.org/manual/Tasks/propertyfile.htmlNomesh DeSilva
@NomeshDeSilva Ok now I will read the article that you posted. But what do you exactly mean when you say that "I have to have it at the same level"? At the same level of the original project (into the IDE) or all the properties file at the same level? Or what=AndreaNobili
No, I'm saying your current structure is absolutely correctNomesh DeSilva

1 Answers

1
votes

It is actually a good idea to put your properties file in the JAR. But the disadvantage of putting it inside the JAR is that it will be harder to edit (you'd have to extract it from the JAR and put it back in).

When you build the JAR file, just include it together with your class files. You can use the method getResourceAsStream() in class Class or ClassLoader to read your properties file inside the JAR file. Suppose, for example, that you have your properties file in the root of the JAR file, then you could read it inside your program like this:

InputStream in = this.getClass().getResourceAsStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();

note : regarding the "path" to your properties file is relative to where your main class is. So, if your main is in the package "com.abc" and the properties file is config.properties, you place the config.properties outside "com" when creating the jar and path is simply "/config.properties"