3

I am learning maven , though worked on ant in past.

I have am just trying to figure out what happens when command mvn install or mvn compile is executed. I am mainly interested how project is built like location of resources to pick for build and where to put them once built

My project source structure is

src > main > java > java files
src > main > resources > reources like spring config atc
src > main > webapp > static files like js,css etc
src > main > webapp > WEB-INF > web.xml and jsp files

Once i give mvn package or mvn clean i see below exploded directory(my focus is on this) with name myProject alongwith other files lile war, classes etc. Explode structure for myProject is

1) All files from (src > main > webapp) including WEB-INF gets copied under myProject
2) All files from (src > main > resources) gets copied under myProject > WEB-INF/Classes
3) All files from (src > main > java) gets copied under myProject > WEB-INF/Classes

As per my understanding when we give any of mvn install or mvn compile or mvn package all compile phases gets executed. But My question is how Maven know where to put the source file under exploded directory. Is it a standard maven follows?

Here is snippet for reference from pom.xml i am using

<artifactId>myProject</artifactId>
    <packaging>war</packaging>
    <name>myProject</name>
.....

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>exploded</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <overlays>

                    </overlays>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>yuicompressor-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jslint</goal>
                            <goal>compress</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <aggregations>
                    </aggregations>
                </configuration>
            </plugin>
        </plugins>
    </build>
M Sach
  • 33,416
  • 76
  • 221
  • 314

1 Answers1

2

Maven follow Java standard when it put compiled files in WEB-INF/classes.

Illustration from the The Java EE 6 Tutorial

This directory contains server-side classes: servlets, utility classes, and JavaBeans components. These classes are only visible to the servlet and are not public.

From Java Servlet 3.1 specifications :

10.5 Directory Structure

A special directory exists within the application hierarchy named “ WEB-INF ”. This directory contains all things related to the application that aren’t in the document root of the application. Most of the WEB-INF node is not part of the public document tree of the application. Except for static resources and JSPs packaged in the META- INF/resources of a JAR file that resides in the WEB-INF/lib directory, no other files contained in the WEB-INF directory may be served di rectly to a cl ient by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext , and may be exposed using the RequestDispatcher calls. Hence, if the Application Developer needs access, from servlet code, to application specific configuration information that he does not wish to be exposed directly to the Web client, he may place it under this directory.

Community
  • 1
  • 1
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Thanx alain. Two clarification on point 10.5 . First:- I know client can not access any file under WEB-INF folder. But looks like from point 10.5 that it is possible fot static resources and jsps provided its under WEB-INF/lib/META- INF/resources. Right? – M Sach Dec 28 '14 at 12:38
  • Second :- 10.5 says "However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext ". My understanding is servlets lying under WEB-INF/classes can access any class/resource under WEB-INF/classes or WEB-INF/lib folder without any difficulty. Then why it is said here "servlet code can access the contents of the WEB-INF directory using the getResource and getResourceAsStream method calls on the ServletContext" ? – M Sach Dec 28 '14 at 12:50
  • @MSach For the first point, yes you're right but the exception is not on `WEB-INF/lib/META- INF/resources`but on META- INF/resources of a JAR file that resides in the WEB-INF/lib. That is not exactly the same thing, you missed the JAR. For the second point the spec just specify how it should be accessed. Even if it's true that your servlet can access everything under WEB-INF/classes, you should use these methods or you'll encounter issues after packaging your app (in a war or ear for exemple). – alain.janinm Dec 28 '14 at 14:24
  • Thanks your right. I missed jar.Correct statement will be "it is possible for client to access static resources and jsps provided its under WEB-INF/lib/META- INF/resources". For second point i am still confused why i will encounter issues after packaging your app. Servlets are also the java classess and if they are lying also under WEB-INF/classes along with other classes, why developer meed to use special methods like getResource and getResourceAsStream to access any class or resource ? – M Sach Dec 28 '14 at 14:34
  • @MSach For exemple if you try to access an image using `new File()` or read a file using an InputStream, it won't work because the image/file are not on the file system but in the war or ear. Hence you have to use these methods to access them. This answer explain pretty well what is getResource() : http://stackoverflow.com/a/14089228/1140748 – alain.janinm Dec 28 '14 at 14:52
  • oh got you. It mean its true for any java class be it servlet or any service class – M Sach Dec 28 '14 at 14:57
  • Yes, basically for all applications that need to be "portable". Even for a Swing desktop app you will have to use getResource to load images or whatever from the JAR. – alain.janinm Dec 28 '14 at 15:04