My Java project uses Gradle. I'd like to include a configuration folder conf when distributing the application with the task distZip.
This is an excerpt from my current project structure.
src
|-- dist
|-- conf
|-- application.properties
|-- keystore.jks
|-- truststore.jks
The conf folder is successfully distributed.
In my main() I load the properties with new File("src/dist/conf/application.properties") which works fine.
The application.properties contains two properties:
keystore.location = ./keystore.jks
truststore.location = ./truststore.jks
When starting the application from IntelliJ it works finde but when starting the distribution it cannot find the keystore.jks and truststore.jks because src/dist/conf does not exist but only conf.
How can I make sure that these files are found?
Your src/dist/conf folder is no resource folder for the java gradle plugin (and IntelliJ). It is just picked up by the application plugin (distZip task). IntelliJ sets the execution directory to the root path of the project. So it is just a coincidence that the src/dist/conf/application.properties can be read.
Move the files in from conf/dist folder to src/main/resources and they will be found by the processResources task and this folder should also be recognized by IntelliJ as a resource folder. Your classpath will have the resource node as root, so that you can use an absolute classpath to get to your files. (straight-forward sample: new File(YourClass.class.getResource("/dist/application.properties").getFile()))
I think that it will take much more effort to read the files in the conf/dist folders, because they are not part of the classpath.
Related
According to the OpenFire documentation (https://www.igniterealtime.org/builds/openfire/docs/latest/documentation/plugin-dev-guide.html) to build a custom plugin I need to create a jar with the following folder structure:
myplugin/
|- plugin.xml <- Plugin definition file
|- readme.html <- Optional readme file for plugin, which will be displayed to end users
|- changelog.html <- Optional changelog file for plugin, which will be displayed to end users
|- logo_small.gif <- Optional small (16x16) icon associated with the plugin (can also be a .png file)
|- logo_large.gif <- Optional large (32x32) icon associated with the plugin (can also be a .png file)
|- classes/ <- Resources your plugin needs (i.e., a properties file)
|- database/ <- Optional database schema files that your plugin needs
|- i18n/ <- Optional i18n files to allow for internationalization of plugins.
|- lib/ <- Libraries (JAR files) your plugin needs
|- web <- Resources for Admin Console integration, if any
|- WEB-INF/
|- web.xml <- Generated web.xml containing compiled JSP entries
|- web-custom.xml <- Optional user-defined web.xml for custom servlets
|- images/
I know there is an Ant build script to help do this but I couldn't find it and I'm having a hard enough time with Gradle and Maven, I'd rather not add having to learn Ant and deal with XML on to my list of chores. So, I tried to make a Gradle build script. Unfortunately Gradle still doesn't make any sense to me and in Intellij it seems to just do whatever it wants.
Regardless, this is the Gradle script I came up with.
task buildPluginJar {
group 'build'
description 'Builds OpenFire Plugin Jar'
println 'Clean old libs and classes.'
delete 'pluginDefinition/lib/*'
delete 'pluginDefinition/classes/*'
println 'Copy libs.'
copy {
into 'pluginDefinition/lib'
from configurations.runtime
}
println 'Copy classes.'
copy {
into 'pluginDefinition/classes'
from 'build/classes'
}
println 'Build jar.'
String outputPath = 'build/out/' + project.name + '.jar';
jar {
into outputPath
from fileTree('pluginDefinition/').include('**/*').collect { zipTree it }
}
}
I managed to get the file copying to work, but the end where it's supposed to put it all into a signal jar isn't working. It completes but there is no jar output. What am I doing wrong?
I could not figure out how to do this with Gradle. But I did figure out how to build jars with Intellij. I followed this tutorial:
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
I went through the File > Project Structure menu and added two artifacts. One to build the jar with all of the java code in my pluginDefinition/lib folder, and another to build a jar with the full contents of the pluginDefinition folder that I could install on my Openfire server.
Still, it would have been nice if I could have done this with Gradle.
[ I asked this question on 29JUL2014, but on 31JUL2014 everything just started working again, and I didn't make any changes. ]
In a Java project (in IntelliJ), I have marked:
src/main/java as Sources Root
src/test/java as Test Sources Root
src/main/resources as Resources Root
src/test/resources as Test Resources Root
I have a file: test/java/Test1.java
in which I try to load a config.properties file that's in src/test/resources, but what gets loaded is the config.properties that's in src/main/resources. I've had other projects where my test resources file loaded correctly. I'm not sure why in this project I'm not making that happen. I do have elsewhere in the project a PropertiesUtil.loadProperties static method that does this:
String filename = "config.properties";
input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename);
Is that why I'm not getting the correct config.properties file? How do I load the test config.properties?
I created sigleton ApplicationConfig for settigs, that reads xml config file. But every time it fails on path to file.
hierarchy:
META-INF
src
--application
----config.xml
--engine
----ApplicationConfig
web
--WEB-INF
----web.xml
I've tried File f = new File("../application/config.xml"); but it gives C:\WebLogic\application\config.xml
It's usually a bad idea to store your configs in 'src'. It's better to separate your code and configuration. I suggest you to read about maven (or gradle).
Basic maven app has pretty simple structure:
src
--main
----java
----resources
where in 'java' folder you store your code, and in the 'resource' folder you store your configs. And now you have problem, because jvm is trying to find your file relatively WebLogic base folder.
And if you use maven, you could just write:
ApplicationConfig.class.getResourceAsStream("/config.xml")
Of course, in this case you should put your config into 'resources' folder.
I have a Spring Configuration java file in my project (i.e. with #Configuration annotation). Now I'm creating a bean of SpringLiquibase in this class. The main file of changelogs i.e. db.changelog.xml file is in resources folder. So directly it's accessible from resources folder if I use springLiquibaseObj.setChangeLog("classpath:db.changelog.xml"); method. But when I try to access other xml files from db.changelog.xml file with <include file="src/main/config/db/db.changelog-main.xml" /> I get error that folder and file is not found.
Here, I want to keep db.changelog.xml file in resources folder and all other new changelog files in config folder which should be at same level as resources. The reason for this is, in resources folder, packages are there so folder structure config/1.0.0 is considered as config/1/0/0 which I don't want.
Is there any way to achieve this structure (shown below) with the current set up in my project?
config
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
resources
db.changelog.xml
or
config
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
db.changelog.xml
resources
I found the solution for this. Before this, I had copied the folder src/main/config into src/main/resouces in netbeans so it took that folder as package. So the folder name - config/db/1.0.0 got changed to config/db/1/0/0, but when I created new folder in resources folder, it worked and all the files in folder 1.0.0 are accessible correctly. Now, my folder structure is like this:
resources
config
db
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
db.changelog.xml
I can access db.changelog.xml file from my Spring class as,springLiquibaseObj.setChangeLog("classpath:db.changelog.xml"); and other changelog files from db.changelog.xml file as, <include file="classpath:config/db/1.0.0/db.changelog-201412120101.xml" relativeToChangelogFile="false" />.
Liquibase uses the application classpath, so as long as config is in your classpath you should be able to load them as well.
They would have a path relative to config, so something like "src="1.0.0/db.changelog.201312120101.xml" in your example.
If config isn't in the classpath, you can use the relativeToChangeLog="true" attribute with a src="../../config/1.0.0/db.changelog.201412120101.xml" but having config directly accessible by liquibase is the cleaner option.
I need some clarification; I have a jar that I built and inside of the jar it has a custom application-context.xml file that I need to load. I load the file within a class inside of the jar. When I am setting the url of this application-context.xml, is the directory specific to my jar's classpath, or still the project that is using the jar-'s classpath?
For example --
Jar's classpath:
src > main > META-INF > application-custom-context.xml
Project's classpath:
src > Libraries > myjar.jar > src > main > META-INF > application-custom-context.xml
I know these url's aren't accurate :P -- But, inside of my context creation, which directory structure would I follow. This is all theoretical. Somebody asked me this today, and I have no clue how to answer it. I would think that if you call a class from a jar, the classpath should be relative to the contents of that jar.. But, what I think is far from always right :)
The "path" to the Spring context is relative to the classpath. If the jar is included in your classpath (it's in web-inf/lib for example), then it would be treated just as if it was in your project (web-inf/classes).
In other words, if your context file is in "META-INF\spring\context.xml" inside your jar file, anyone who includes your jar file can reference it in the same manner.