I'm wondering what is the best way to organize Spring Configurator directories in my Maven project ? I wan't to have prod,dev and test config sets with seperate *.xml and *.conf files.
And also I'd like to have a seperate master *.xml file that is used in every config set.
Do I have to place *.xml and *.conf files in seperate directories ?
Or could I just places them in the same dir , like so:
spring/
test/application.xml
test/params.conf
dev/application.xml
dev/params.conf
prod/application.xml
prod/params.conf
Thanks for help :)
Place them in a directory structure in a way that no redundancy exist on classpath. Eg: if your code searches for */application.xml file and you have all 3 on the classpath it will cause confusion
Also leverage the use of Maven build profile and assembly plugin. Configure it such that only relevant files are packaged for each build profile.
Related
I have a question about the TestNG configuration files and Maven project structure, I would like know the best practices to save the configuration files.
E.g. I have use a testing.xml is a file which uses several classes to create a Suite. If I use the java application archetype of maven, where is the best place to save the xml file (testing.xml)?. I have taken a look to Maven standar directories, but I do not find anything about this issue.
src/test/resources
you can create a folder test-suites here and have different suite xml's here.
I use standard maven directory structure for my project http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
However, this is problematic because configuration files are going to be in jar file. There is some discussion on reading the config files from predefined location as How to get the path of a running JAR file?
But I am trying to package my jar file without any configuration files in it and make the directory structure look similar to any other open source projects that we download
project|
--lib
|
--conf
|
--docs
|
--src
I prefer not to do any source code changes (ex:all my config files are in source path for maven development and I just read the files directly without prepending with the path); if I have to change path to config files in source code, what should I do to make the path work both in development and in production
My questions
1)how can I have current "development" directory structure as is, but have my "production" release directory will look similar to that of any apache(say) project
2)for every release, what changes should I do in pom.xml so that the version numbers are appended to jar and incremented
I prefer not to do any source/java code changes (ex:all my config files are in source path for maven development and I just read the files directly without prepending with the path); if I have to change path to config files in source code, what should I do to make the path work both in development and in production
(I am using Maven and Eclipse)
In my projects I use the maven assembly plugin to package the application in a redistribuable way :
Everything under src/main/java and src/main/resources is packaged inside the main jar (standard behaviour).
Then using assembly plugin, I create a zip containing 3 folders :
/lib : contains the main jar + all the dependencies
/bin : contains the additional files from the src/main/scripts maven source folder (startup shell scripts)
/etc (or /conf) : contains the additional files from the src/main/config maven source folder (config files)
I find this setup very convenient as it is really close to the initial maven standard layout (src/main/java + src/main/resources in the main jar and other folders taken appart), still being adapted to my projects.
You can use your existing resources by mentioning those resource directories in Maven Resource Plugin.
add following code to build ( tag )section of your pom.xml
<resources>
<resource>
<directory>[your folder here]</directory>
</resource>
</resources>
you can add several non maven standard resources over here ( anything other that src/main/resources ).
I want to share some more tips based on what I digged out (#superbob gave good explanation).
This is easier said than done....
I wanted to create jar (executable first) and copy this jar file into zip (along with few other directories for releasing in to production)...it is easy if u can look at existing pom file than fiddling with existing pom.
Look here for an existing project(pom.xml) http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/
In my case, I had multiple pom files and it adds another layer of complexity for newbie like me..look here for multi module example with maven assembly plugin http://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/index.html
Last but not least, run the following command to make sure assembly is created
mvn clean install package assembly:single
I don't want to put my .properties and log4j files in my /resources folder in my spring MVC appication because these will get put into the jar at compile time, and I want to be able to edit these files on my server when I deploy my application.
So I created a folder and dropped my log4j, *.properties files, and my spring-context.xml file in it.
I have a maven multi project, and my folders look like:
/myapp-persists
/myapp-web
/src/main/conf
The /src/main/conf has my property files, so in IntelliJ I went to the myapp-web module and added a 'jar or directories' dependancy, pointed to that folder and chose 'classes'.
It didn't work, my files were not picked up and starting the server resulted in a crash, the property files were not detected.
What am I doing wrong here?
It seems me doing this via intelliJ is going to cause my maven builds to fail then right? I really don't want to create a separate maven project for this.
You can just use the context menu on /src/main/conf and choose Directory : Add as Source. In the preferences you should set keep source folders in the maven settings (not really sure about the naming of the menu/settings - no idea at hand, sorry). The jar dependency is not needed.
I currently started working on a maven web-app project that needs to be launched with the jetty:run-exploded goal for development/debugging in eclipse.
Now, I have an XML file which contents I need to access at runtime. My problem is: where to put the file so that the code that does the reading works both in "exploded" and packaged (i.e. in the WAR) mode?
Putting the file in src/main/java (so as to be in the classpath) won't cut it since maven filters out all non-java files on packaging.
When the file is in src/main/resources, one mean would be to figure out the root path of the project (during eclipse development) and look into that directory - but this won't be the case anymore when the project will be packaged.
Of course I could go into writing code that tries to read the file from both locations, but this seems rather cumbersome. Any suggestions?
Files in src/main/resources are copied to the target/classes directory and are available on the class path. Just read them from the class path. As explained in How do I add resources to my JAR? from the maven documentation (with a test resource here):
In a unit test you could use a simple
snippet of code like the following to
access the resource required for
testing:
...
// Retrieve resource
InputStream is = getClass().getResourceAsStream("/test.properties" );
// Do something with the resource
...
In such case I put the file under src/main/resources directory and use Spring's ClassPathResource. This way the file is accessible in IDE, during Maven build process and in runtime.
Is there a best practice for where configuration files should be stored in a Java project. The file type is a Java properties file in this case, but I do use other file types in other projects.
Would the recommendation vary from stand alone application(.jar) to web app(.war)?
You'll find that many open-source projects follow the directory structure used by Maven. In this setup your application source code is kept in src/main/java, application resources, including properties files, in src/main/resources, and other config files in src/main/config. Files related to unit tests use a similar directory structure; src/test/java and src/test/resources.
Personally I tend to use this layout because of its widespread use. I also keep an "etc" directory beneath the project root to house files that aren't directly related to the application. For example, I keep configuration files for PMD and Checkstyle in etc.
In general a common practice is to have a resources directory for configuration files which is copied into the build artifact by the build process. Maven uses this in its default project structure. Within the resources directory, you might also have a META-INF directory and/or a WEB-INF directory in an application packaged as a war.
I use:
META-INF/ for jar files
WEB-INF/ for war files