In project A I have a dependency to project B which has the CSS resources directory.
Due to some framework issues I need to copy the CSS directory to project A during the build. How to do it? The css directory is inside src/main/resources/css therefore it goes to /css inside the .jar
No need to copy because Servlet 3.0 Specs sais in part 3.6, you can place the css in META-INF/resources into the jar of your project B.
It works great except maven-tomcat-plugin.
This is where the file ends up:
<outputDirectory>${basedir}/target/blah</outputDirectory>
This is where it is copied from:
<directory>src/main/otherresources</directory>
There would be an or tag to tell the file name(s)
Multiples
You need multiple executions with different ids for multiple folders:
There are two solutions. But before you have to know the path from project A to project B.
You can use the maven resource plugin.
There's a goal to copy a resource to an output directory.
Or, you can use the maven ant plugin.
Here is an example : http://www.javacodegeeks.com/2013/07/how-to-run-ant-targets-with-maven.html
Related
I have a Maven project A (packaged as a pom) containing Maven module project B (packaged as a jar).
Project B is also physically in the root of project A.
When B is being built, a plugin indirectly uses my code (inside B) to access a property file in its src/main/resources location.
When I build project B directly (mvn clean install) the code can easily find that file by using new File("src/main/resources/foo.properties");
However when I try to build project A, it first will try to build the module B, and in that case it cannot find the property file.
Apart from the 'new File' variant, I have tried using
this.getClass().getClassLoader().getResourceAsStream("src/main/resources/foo.properties");
and I tried using Spring:
Resource resource = new ClassPathResource("src/main/resources/foo.properties");
Both also with a "/" prefix. But the file simply cannot be found.
Why is that? Might it be looking for the file in the root of A? Is it possible to find the file in module B when building A?
Thanks!
new File("src/main/resources/foo.properties");
This is a path relative to the working directory. The working directory is wherever you called maven. So that's why it won't work when calling maven anywhere other than the directory of module B.
this.getClass().getClassLoader().getResourceAsStream("src/main/resources/foo.properties");
This won't work because the classpath is where maven put the classes, that is, target/classes.
What you need to do is add this file as resource in the maven build, then use the getResourceAsStream idea with the correct path. Since you used the standard maven layout, you probably don't need to anything and just use
this.getClass().getClassLoader().getResourceAsStream("foo.properties");
I am new to Spring and just created a project using Spring CLI.
When I opened the project using STS i see the following folder structure.
There are two src folders.
What is the difference between them ? src/main/java and just src.
Also suppose I want to create a new folder like webapp under main, where should I create it ?
I looked a lot online but couldn't find any answers.
Please help.
There are two src folders
No, it's just one src folder and what you see is a way of displaying a Maven project under Package Explorer view of eclipse/STS. Change the view to Navigator and you find that it's just one src folder.
If you want to create a new folder "webapp", please don't. If you use Spring, you won't need it !
How you should read this project:
src/main/java is where you write your .java files that will go in production
src/main/resources is where you will put your *.properties file (mostly) and other files, that are not .java files.
src/test/java is where you write your .java file that will run tests against your src/main/java/**.java files.
target is where your java files will be compiled. You will find your .classes file, but also the files inside src/main/resources. Those files will be inside your .jar when you decide to create one.
You think you see 2 src folder, but in reality, it is your IDE that gives you this. If you go on your file system, I am sure you will have only one src folder. It is your IDE, thanks you maven and some default configuration, which "flags" different folder in category, to be able to compile and run your tests. There is 4 categories :
sources: src/main/java
resources: src/main/resources
Test sources: src/test/java
Test resources: src/test/resources (sometimes)
If you to build a webapp using Spring, go read this: https://www.baeldung.com/spring-mvc-tutorial. It is well explained, and you will be able to run a project in a short time.
We start from the end, after the build install, in the target folder, is possibile to see 2 important thing: the file producted from the build (war, ear, jar) and a folder called usually like the project. My question is, the file is done from the folder ? Or the folder is just a copy to see faster the result of the build ?
In a typical build, maven places in target/generated_folder all required builds assets(compiled classes, resources e.t.c).
Then, depending on the declared packaging, compresses aforesaid folder and generates the final artifact.
So to answer you question, the final generated file is derived from the folder inside target directory.
Every Java project I download has main and test folders. Both contain java and resources sub-folders. Some projects even add more levels: for instance dir1, dir2, dir3 and etc in the root, where each dirX contains src with main and test subfolders. How do I handle them? If I just drop the content of the project root into the default src, Eclipse will attribute classes into dirX.src.main.java.com.pkgY obviously resulting in "The declared package com.pkgY does not match the expected package dirX.src.main.java.com.pkgY" error. Am I supposed to add every dirX/src/java directory as a source separately in buildpath, as this answer suggests? What about the corresponding tests? Thanks.
This structure is Maven's Standard Directory Layout. In your case, you need to add both src/main/java and src/test/java (right click on java folder and go for use this as source folder) and resources as well to classpath through configure buildpath option. Refer this maven link for more details on this.
Make sure you installed m2e (http://download.eclipse.org/technology/m2e/releases), and if you're doing JavaEE stuff with Eclipse WTP, you should also probably add m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)
Then, you have 2 solutions :
either right click on each project and Configure > Convert to Maven project
remove the projects from your workspace and do an "Import as existing maven project"
m2e will take care of configuring Eclipse based on your pom.xml configuration.
I am going to be using Subversion for source control on a new J2EE web application. What directory structure will you recommend for organizing code, tests and documentation?
I usually have
Project Directory
src - actual source
doc - documentation
lib - libraries referenced from source
dep - installation files for dependencies that don't fit in lib
db - database installation script
In work with Visual Studio, I'm not sure if this works the same in the java world. But i usually put stuff in different project folders in src. For each source project there's a separate test project. Build files go in the main project directory. I usually put a README there too documenting how to setup the project if it needs more than just checking out.
EDIT: This is the structure for a single working checkout of the project. It will be duplicated for each branch/tag in your revision control system (remember, in most SVN system, copies are cheap). The above example under Subversion would look like:
/project
/trunk
/src
/doc
/...
/branches
/feature1
/src
/doc
/...
/feature2
/src
/doc
/...
I found some old questions here on SO that might be interesting for you:
Whats a good standard code layout for a php application
Contains a link to an article on Scalable and Flexible Directory Structure for Web Applications (focus on PHP, though)
How to structure a java application, in other words: where do I put my classes?
Structure of Projects in Version Control
To expand on what Mendelt Siebenga suggested, I would also add a web directory (for JSP files, WEB-INF, web.xml, etc).
Tests should go in a folder named test that is a sibling of the main src folder - this way your unit test classes can have the same package name as the source code being tested (to ease with situations where you want to test protected methods or classes, for example... see the JUnit FAQ for this, and this question also on Where should I put my test files?).
I haven't had much use for it myself, but a Maven project will also create a resources folder alongside the src folder for non-source code that you want to package/deploy along with the main source code - things such as properties files, resources bundles, etc. Your mileage may vary on this one.
I use Eclipse for creating J2EE web applications and this will create the following project structure:
WebAppName\
\lib
\src
\tests
etc...
I would then create an SVN folder on our trunk called WebAppNameProject. Within this folder I would create folders called WebAppNameSource, Documentation etc. Within the WebAppNameSource folder I would place the project source generated by Eclipse. Thus I would have the following folder structure in SVN:
\svn\trunk\WebAppNameProject
\WebAppNameSource
\lib
\src
\tests
etc...
\Documentation
Hope this helps.