I have some additional text files under grails-app/conf that I am reading explicitly with java.io.File. I am getting the base directory for conf using the classpath but then opening the files directly.
Specifically - I have a folder grails-app/conf/folder and reading a file like this:
new File(new File(this.class.getResource("/folder/").getFile(), "file.txt")
folder/file.txt is actually read from ~/.grails/2.2.2/projects/myProject/resources/folder
How do I get grails run-app to push these txt files out when changed, similar to .groovy files?
Related
I'm Setting up Log4j2 in a Spring-boot application. I now want to create a /log directory exactly where the .jar file is located.
This is needed as we start the java application from a startup script and the configuration should work on both windows and unix developer machines as well as a server.
I already tried with:
<RollingFile name="FileAppender" fileName="./logs/mylog.log"
filePattern="logs/mylog-%d{yyyy-MM-dd}-%i.log">
which just creates a log folder at the directory where the jar gets started.
then I read i should use .\log/mylog.log as .\ points to the directory of the jar file.
But then it just creates a folder called .\log.
I also tried with configuration with jvm arguments and calling them at the log4j2.xml with: ${logFile}. Now a directory gets created called '${logFile}.
The only ${} command working is the directory of the log4j configuration file. But as this is inside the jar it just gets me a pretty useless folder structure
Thanks in Advance
EDIT: In the End what I did was setting up two configuration files, log4j2.xml and log4j2-prod.xml
The log4j2.xml took the system property as Vikas Sachdeva mentioned, while the prod.xml got the location hard coded.
Not really the solution I was looking for but made it work.
One solution is to pass log directory location through system properties.
Configuration file will look like -
<RollingFile name="FileAppender" fileName="${sys:basePath}/mylog.log"
filePattern="${sys:basePath}/mylog-%d{yyyy-MM-dd}-%i.log">
Now, pass VM argument basePath with absolute path of directory containing JAR file -
java -jar myapp.jar -DbasePath=/home/ubuntu/app
def encryptedUid = getClass().getClassLoader()
.getResourceAsStream('user.txt')
.getText()
This code in the dataSource.groovy file works fine when I run it in a windows environment, but when I check the code in and Jenkins tries to load DataSource.groovy I get:
Error loading DataSource.groovy: Cannot invoke method getText() on null object.
The user.txt file is in the root of the src/java folder, and I know that it is built into the war file in a windows build. It just doesn't even get to building the war file on the Linux box.
Any ideas?
Apparently you're trying to configure the database username/password, but don't want to put them in DataSource.groovy directly for security reasons. Here's how I handle this:
Put the secret configuration in a file /grails-app/conf/secret.properties. The contents of this file are shown below:
dataSource.username=root
dataSource.password=secret
# other secret configuration
Include this file in the grails configuration by adding the following to Config.groovy
grails.config.locations = ["classpath:secret.properties"]
If you want to be able to override the config. in secret.properties on a per-environment basis, change this to
grails.config.locations = [
"classpath:secret.properties",
"classpath:secret-${Environment.current}.properties"
]
You can then (optionally) add a file secret-DEVELOPMENT.properties that will override the configuration in secret.properties in the dev environment, and likewise for the other environments.
Of course, in order for this to work, the secret*.properties files must be present when building the war (or executing run-app) and should not be checked into VCS.
You're not restricted to placing these config. files in a relative location to the classpath. You can put them anywhere on the file system by using the file: prefix instead of classpath:. Finally, you can put the secret configuration in a .groovy config. file instead of a .properties file.
In the Config.groovy file there is the setting:
grails.serverURL = "http://www.changeme.com"
Is there a way for a grails war file to be produced such that it can handle
"http://www.site1.com" and
"http://www.site2.com"
?
Per Ivar's comment response:
remove grails.serverURL and use container configuration
This did work. I happen to be running jetty 9. In case you are too, there is an example test.xml file in the webapps.demo directory. One can copy this in the webapps directory, rename it to the war name you are using, e.g. root.xml, and then just have a few entries in it:
set the name of your war file, e.g. root.war
fill in the virtualHosts area.
When I deploy my grails generated war file to jetty I get the following error. My question is how de we tell grails where to look for this config file? It works fine for grails run-app because its run from project base where this file exists but gives this error when running withing jetty.
org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper - Unable to load specified config location file:./grails-app/conf/something-config.properties
If you're bundling the file in with the war, then putting it in grails-app/conf should put it in the root of the classpath. Move it to src/java if it doesn't, since that will definitely work both with run-app and in a war. Then register it as an external config in Config.groovy:
grails.config.locations = ["classpath:something-config.properties"]
If you want to deploy it separately from the war (for example to have one war that works in multiple deployments each with its own config file) then you would make the same change to Config.groovy, but copy it to somewhere in Jetty's classpath. I'm not that familiar with Jetty, but I know that Tomcat's lib dir is in its classpath, so I put files like this there. I assume there's an analagous location for Jetty where you can put jars and resources that should be loaded.
You can use externalized configurations
It's a good question. My Config.groovy has these lines commented out. I wonder if yours has a special external config you are trying to read:
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
// grails.config.locations = [ "classpath:${appName}-config.properties",
// "classpath:${appName}-config.groovy",
// "file:${userHome}/.grails/${appName}-config.properties",
// "file:${userHome}/.grails/${appName}-config.groovy"]
// if(System.properties["${appName}.config.location"]) {
// grails.config.locations << "file:" + System.properties["${appName}.config.location"]
// }
I have a java project and I am using hibernate.The thing here is I want to place the hibernate.cfg.xml file outside "src" folder, but when I am configuring this file it is showing FileNotFoundException. If I put it inside src folder its ok. But I want to separate the java src and all config file in separate folder.
root
|____src
|____conf
|____mapping
|____(all xml file with hibernate.cfg.xml)
SessionFactory _sessionFactory = (new Configuration()).configure("./conf/mapping/hibernate.cfg.xml").buildSessionFactory();
Its showing exception......
Add conf/mapping directory to your CLASSPATH and load the configuration file
new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
or just
new Configuration().buildSessionFactory();
as this is the standard name.
Regardless there would be no ./ at the beginning of the resource path.
What you want to do is
include conf/mapping in your project build path(With Eclipse properties->javabuild path->source->add Folder)
then no need to precise the hibernate config file in your code
new Configuration().buildSessionFactory(); is enough
This manipulation is simple and works like a charm.