Personalised directory structure deploying with Maven - java

I have just recently started working with Maven and I have been asked to create a specific structure for when we deploy. The structure has to look like this:
/opt
\-myproject
|- libs
|- conf
\- logs
libs - Contains all the dependent libs and our own myproject.jar
conf - Contains the .properties files
logs - Contains our log file myproject.log
Our current java structure looks like this:
/src
|-myprojectpackages
propertiesfile1.properties
propertiesfile2.properties
pom.xml
My question is, do I have to change the java structure or is there any way to specify it in the pom.xml file? How should I handle this with Maven?
Edit:
In the end I used the Maven default layout, as advised in the comments section.

Related

Coping properties file automatically with Gradle to the same directories after the build

I am working on a Java project that contains many properties files and the structure looks like this:
src
|
-main
|
-java
|
-ui
|
-many directories with property file in each directory.
I want to build fat jar using Gradle build that will contain those files in the same directories.
Something like:
build
|
-classes
|
-java
|
-main
|
-ui.... and all the files like above.
How can I to do it?
By convention gradle will package all the properties (and other resource) files placed under src/main/resources into the final jar artifact.
If you are sure that you still want to place your properties file under src/main/java then you can configure resources source sets location using the following snippet in your build.gradle
sourceSets {
main {
resources {
srcDirs = ["src/main/java"]
include "**/*.properties"
}
}
}
The properties files will not end up in build/classes but will be part of you jar. You you want them to be build/classes/java as well (not recommended), then you need to configure the output dir for resources as well. See this answer.

Creating JAR Files with Maven: How to change the target directory for class files?

When creating a JAR file using Maven, the class files (or rather the package structure) are placed directly in the root of the JAR file. How can I configure Maven (or the maven-jar-plugin) to copy the classes to a "classes" directory instead? For example, the jar file should have the following structure:
- classes
- com
- mycompany
- myapp
- HelloWorld.class
- META-INF
- MANIFEST.MF
The structure of the sources should not be changed (it's the Maven default structure).
Many thanks for any help.

Configuring log4j2 in a multi-module J2EE web project without maven

I've been searching the web and SO for a while now to find the answer for my question, but everything I could find addressed multi-module projects that were implemented with Maven. My project does not use maven and so it doesn't help my scenario.
I'm building a Java 8 J2EE web services project that runs on Tomcat 8 and is broken into a few separate projects (see project tree below). I am not using maven, I'm defining all of the dependencies through eclipse (I am hoping to learn about maven at some point and "mavenize" my projects, but I'm not there yet).
How can I achieve the following:
Have a single log4j2.xml file that would be used by the dynamic web project and all the Java sub-projects. The only thing that has worked for me so far is to have a copy of the log4j2.xml file present in the src folder of each of the projects.
I've attempted various things, such as placing the configuration file in a shared folder and adding the folder to each of the projects' class path, or placing the file in the CommonLib project, which is used by all, but each time I keep getting a log4j warning that no configuration file was found.
Here's my project tree:
Services (Dynamic web project)
^
|__ BusinessCore (Java project)
| ^
| |___ DAO (Java project)
| | ^
|_________|_____|__CommonLib (Java project)
It sounds like you're trying to use log4j2 automatic configuration by including the log4j2 config file on the classpath.
I further assume that you're trying to run your code from within Eclipse. The only way it will work correctly with automatic configuration (without specifying the path to the config file via system property) is if the log4j2 config file is inside of a "source folder" that is included on the build path or if you put your config file inside of a jar and add that jar to your build path.
So, assuming you're using a log4j2.xml file, if you place log4j2.xml inside of a source folder of your CommonLib project and include this project on the build path of your other projects (along with the necessary log4j2 jars) it should work fine.
Here is how I have set up an example project:
Content of root level project folder non-maven-web-project-log4j2:
Content of BusinessCore:
Content of CommonLib:
Note that the "config" folder is a source folder (you will see this again later) in Eclipse and that the log4j2.xml is contained in the config folder.
Build Path configuration:
non-maven-web-project-log4j2 has a dependency on the BusinessCore project
BusinessCore has a dependency on CommonLib (no screenshot, it's almost identical to the previous)
CommonLib has an extra source folder called config:
and this config folder contains the log4j2.xml file:
Each project has one simple class in its source folder that references a class from its child project. In the non-maven-web-project-log4j2 project there is a class that refers to the class in BusinessCore, and in BusinessCore there is a class referring to the class in CommonLib. Every class generates a log4j2 message. When I run the class from the root project (non-maven-web-project-log4j2) I am able to see all of the logs and the logs have the pattern I specified in my log4j2 config file which confirms that log4j2 is able to find its configuration.
As a final note - I added the extra "config" source folder just to emphasize the point that you can place your log4j2 config file in any source folder in the build path, it is not necessary to create a separate folder for it if you don't want to - you could instead just include it in the src directory as you already noted in your question.

Gradle hides files on building war

My project tree:
/project
|
|--> /src
| |-->/main
| |-->/webapp
| |--> a lot of folders here including /files with doc file inside
|--> build.gradle
when i put in my build.gradle file only apply plugin 'war' it puts everything in war file on build except doc file from /files directory
I read Gradle docs so I'm adding
war{
from 'src/main/webapp/files'
}
to my build.gradle file, after it my war file became bigger in size but needed file is not there.
How can I add file from /files directory to war /files directory?
UPDATE
After adding this project to multi-project build the problem has gone. Don't know really what was wrong.
I believe the syntax you're looking for is:
war {
from 'src/main/webapp/files' into 'files'
}
This will copy everything from src/main/webapp/files into a subdirectory called files at the root of your WAR file (eg. my-app.war/files)
I had a lot of interesting issues with Gradle's war task and copying, so if this doesn't work, I can look up what other methods I've used.

maven webapp to place jsps in /WEB-INF/jsp

I have inherited a webapp built using NetBean's internal ant.
All jsps reside in:
WEB-INF/jsp
And the web.xml has hardcoded links to /WEB-INF/jsp/somefile.jsp
How can I use the maven war plugin to place the JSP there, maintaining consistency with the current structure ?
My pom currently reads:
<warSourceDirectory>${basedir}/web/WEB-INF</warSourceDirectory>
What the problem? Let's look at a standard war project structure:
$ mvn archetype:create -DgroupId=com.mycompany.app \
> -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
...
$ tree my-webapp/
my-webapp/
|-- pom.xml
`-- src
`-- main
|-- resources
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp
5 directories, 3 files
No need to configure anything, just put your JSPs under src/main/webapp/WEB-INF/jsp and there you go.
EDIT: I don't understand why you have the following line in your maven-war-plugin configuration:
<warSourceDirectory>${basedir}/web/WEB-INF</warSourceDirectory>
What is the expected behavior? Why don't you use the default value ${basedir}/src/main/webapp?
Also note that everything in src/main/resources will be "on the classpath", i.e., it all is copied to my-webapp/WEB-INF/classes when the war is built. So that's a good place to put your configuration files, for example, log4j.xml / logback.xml, or Spring's applicationContext.xml and Spring's other config files, which you can then easily reference with classpath:somefile.xml.
What also makes this very nice is you can set up filters in maven so that it transforms the files from src/resources before it puts them in the war file.
So if you have any config files, other than web.xml, in src/main/webapp/WEB-INF, think about moving them to the src/main/resources directory.

Categories