I'm trying to create a Spring project structure where I have my HTML templates stored in the same src/main/java package (in Tapestry-like way), so the structure basically looks like this:
src/main/java
-org
-example
-views
-pageOne
PageOneController.java
Template.html
-pageTwo
PageTwoController.java
Template.html
However after packaging a WAR while using Maven I get a structure like this:
WEB-INF/classes
-org
-example
-views
-pageOne
PageOneController.class
-pageTwo
PageTwoController.class
So the .html files are ignored and obviously nothing works.
I tried to configure maven-resources-plugin to copy those resources like this:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>*/**.html</include>
</includes>
<excludes>
<exclude>*/**.java</exclude>
</excludes>
</resource>
</resources>
</build>
but to no avail.
How do I configure my project to copy non-java files from src/main/java into the final output?
It turns out that the problem was with maven modules. I have declared resources plugin in the main pom.xml instead of the module's pom.xml which lead to the configuration issues.
Related
I'm trying to package a Spring Project with Maven, but the Beans.xml file is missing.
A lot of people suggest to put the Bean.xml file in the main/resources, but it does not work for me. I think that only works for Eclipse user and I'm just compiling in the terminal using 'mvn clean install'.
Stupid mistake, inside my pom file the directory was like the following:
<build>
<resources>
<resource>
<directory>src/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
My resources folder was not inside src/main/resources, but src/resources.
And when I open my jar file the Beans.xml file this there and the code runs without error.
I have declare a property in pom.xml
<PROPERTY_KEY>d:/../.../abc.properties</PROPERTY_KEY>
and then used
<plugin>
<configuration>
<replacements>
<replacement>
<token>APP_PROPERTY</token>
<value>${PROPERTY_KEY}</value>
....
</plugin>
and have used APP_PROPERTY in my dispatcher-servlet.xml and in controller classes as well. It's working fine as in this case control comes through web.xml and this web.xml has an entry for my dispatcher-servlet.xml.
But when I want to use this same APP_PROPERTY in my JUNI test class, it is not getting resolved.
I have to create a new dispatcher-servlet-test.xml file (and put it under /src/main/resources folder) as my actual dispatcher-servlet.xml is not working from JUnit test class. Now my JUnit test class is able to pick dispatcher-servlet-test.xml. But, it is not able to resolve properties (APP_PROPERTY) that I have defined in my pom.xml.
I'm using SpringJUnit4ClassRunner.class in my test class.
What should I do to get these property resolved?
Resources for tests need to live under src/test/resources, you should move your dispatcher-servlet-test.xml there. Once moved, it's likely not being filtered as a resource, the stanza you want is something like:
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/dispatcher-servlet-test.xml</include>
</includes>
</resource>
You may already have a similar stanza in your POM, or if you're working on a larger project the parent pom.xml may have a stanza declaring:
<filtering>true</filtering>
I have a maven Java project projA that build into a jar.
I have another maven Web project projB that holds a dependency of projA.
In proj A one of my classes accesses the resources located in the packages in projA.
When I expand the jar shown under dependencies in projB, I see that it does not contain the resource files (.dat).
What setting I need to do to copy these files into the jar when I build my projA in Netbeans
I am using NetBeans IDE 8.0.2 / Java 1.8
In your pom.xml, include something along the line like this:
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**/*.dat</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
[...]
I have Java project made with maven. So I have typical maven project layout. And I use Jython. So I got few python files. Wich I use through PythonInterpreter in Java classes.
I place my python files in src/main/py folder. And I use this path to import the modules by interpreter. It works fine on my laptop.
The problem is:
When I do mvn install, this folder does not goes to the war.
I read about maven resources plugin and added this folder as a resource. Like this:
<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
I that case it adds everything that folder contents, to web-inf/ directly, but not in the src/main/py. So that path is invalid for application in war archive.
Question is:
How should I place this python resource and what I should write in pom.xml, to be able to use the same path on the laptop, and the server?
Can you try including targetPath in the resource element as below:
<resource>
<targetPath>../</targetPath>
<directory>src/main/py</directory>
<includes>
<include>**/*.py</include>
</includes>
</resource>
I would suggest to use the src/main/scripts folder and check if they where packaged into the war if this does not work check the documentation of the maven-war-plugin and define it with the war plugin as described in the documentation.
<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
is actually enough to include into jar/war file.
For running see
Compile Python Sources in Maven
It is not recommended to use src/main/py in your python script,
think as src/main/py as working/current folder for you python
Where should I put the log4j.properties file when using the conventional Maven directories?
src/main/resources is the "standard placement" for this.
Update: The above answers the question, but its not the best solution. Check out the other answers and the comments on this ... you would probably not shipping your own logging properties with the jar but instead leave it to the client (for example app-server, stage environment, etc) to configure the desired logging. Thus, putting it in src/test/resources is my preferred solution.
Note: Speaking of leaving the concrete log config to the client/user, you should consider replacing log4j with slf4j in your app.
Just putting it in src/main/resources will bundle it inside the artifact. E.g. if your artifact is a JAR, you will have the log4j.properties file inside it, losing its initial point of making logging configurable.
I usually put it in src/main/resources, and set it to be output to target like so:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
</build>
Additionally, in order for log4j to actually see it, you have to add the output directory to the class path.
If your artifact is an executable JAR, you probably used the maven-assembly-plugin to create it. Inside that plugin, you can add the current folder of the JAR to the class path by adding a Class-Path manifest entry like so:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.your-package.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now the log4j.properties file will be right next to your JAR file, independently configurable.
To run your application directly from Eclipse, add the resources directory to your classpath in your run configuration: Run->Run Configurations...->Java Application->New select the Classpath tab, select Advanced and browse to your src/resources directory.
Some "data mining" accounts for that src/main/resources is the typical place.
Results on Google Code Search:
src/main/resources/log4j.properties: 4877
src/main/java/log4j.properties: 215
The resources used for initializing the project are preferably put in src/main/resources folder. To enable loading of these resources during the build, one can simply add entries in the pom.xml in maven project as a build resource
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Other .properties files can also be kept in this folder used for initialization.
Filtering is set true if you want to have some variables in the properties files of resources folder and populate them from the profile filters properties files, which are kept in src/main/filters which is set as profiles but it is a different use case altogether. For now, you can ignore them.
This is a great resource maven resource plugins, it's useful, just browse through other sections too.
When putting resource files in another location is not the best solution you can use:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<build>
For example when resources files (e.g. jaxb.properties) goes deep inside packages along with Java classes.
If your log4j.properties or log4j.xml file not found under src/main/resources use this PropertyConfigurator.configure("log4j.xml");
PropertyConfigurator.configure("log4j.xml");
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.error(message);
Add the below code from the resources tags in your pom.xml inside build tags.
so it means resources tags must be inside of build tags in your pom.xml
<build>
<resources>
<resource>
<directory>src/main/java/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<build/>