views:

114

answers:

1

Hello everyone, I read some useful posts here on SO about previous maven questions, I'm currently very interested in learning maven(cause I like it and because my boss requires me to). I'm currently reading [this][1] book and I'm working my way trough examples. Its a straightforward book but its has some errors inside(trivial ones), yet for a newbie like me can be hard to spot, once spotted it can be easily fixed. Is there any other book better to understand maven from top to bottom?

Second part of the question is relating an example in this book, maybe a simple explanations would resolve my doubts.

Here is the thing, I made a simple-weather project in java which retrieves the weather conditions from yahoo weather server, given the particular zip code it returns weather information.

Then I made an 'simple-webapp'(with maven as well as the one above I forgot to mention that), which is basicaly a web project which has some default servlet already there with maven and it does nothing.

And I have some parent-project I wanna merge those two projects into one, so I made a pom.xml which has 2 modules , 1 to retrieve info(java project) and other to display it on the web (web app).

I made everything work at the end, but here is the odd thing .. if I make webapp display any string "name" lets say then build it independently, it does exactly print that string. But when I put the webapp in the "parent-project" and change this string to "name1" and build it as sa partent-project sub module.. nothing changes ..

So I go back to the point, because simple-webapp is dependent on simple-weather I can't build it anymore on its own, so now if I wanna make some changes to the webapp.. modify the webapp outside the "parent-project" build it there then paste it back to the parent-project and then the changes will apply, why is that, why can't I directly change the servlet content/or add another one in the webapp as the part of the "parent-project"?

Thank you.. I know its a long and boring question, but I'm just trying to learn things and there is no better place to ask than here :D

EDIT - HERE ARE POM FILES FOR EACH PROJECT :

1. simple-parent pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.sonatype.mavenbook.multi</groupId>
    <artifactId>simple-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>Multi Chapter Simple Parent Project</name>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2. simple-weather pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd"&gt;
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-weather</artifactId>
<packaging>jar</packaging>
<name>Multi Chapter Simple Weather API</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

3. simple-webapp pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd"&gt;
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-weather</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
+3  A: 

I am not sure to completely understand your question. However, let's explain some principles in Maven.

So you have such a structure:

parent
  + simple-weather
  + simple-webapp

On a Maven point of view, we have 3 projects here:

  • parent, which is a pom project (i.e. its packaging attribute is set to pom)
  • simple-weather, which is a jar project and has parent as parent.
  • simple-webapp, which is a war project, has parent as parent and simple-weather as dependency.

The parent projects uses two concepts in Maven:

  • The inheritance, which say that all of his children (simple-weather and simple-webapp) will inherit all of his properties (this concept is almost the same thing as the extends in Java).
  • The aggregation, which is defined by the definition of <modules>. Aggregation means that every command that will be run on the project will also be run on each module.

What happen if I build (using mvn clean install) on the parent directory?

  1. Maven will "compile" the parent project and then install the pom.xml in the local repository.
  2. Maven will compile the simple-weather project, but as it has a parent, Maven will look the parent pom.xml file into the local repository. Once the JAR is created, it is installed in the local repository.
  3. Maven will finally compile the simple-webapp project. Maven will do the same thing for the parent pom.xml, but also for the simple-weather project.

The situtation explained in the 3rd point is important: If you want to build the simple-webapp project, Maven will always try to find all of his dependencies - including simple-weather - from the local (or distant) repository.

That's why if you build only the simple-webapp without building and installing simple-weather, Maven will not find the latter project, or will find an older version.

So to summarize, when you work on multi-modules project with Maven, try to always run the build and install commands from the root (or parent) directory.

I hope that this explanation is clear enough and help you to understand what happen in your case. Do not hesitate to ask more information...

romaintaz
Thank you for your explanation, that sure helped clarify some things for me, but I didn't quite get the answer to my question. Here is the thing, if I wan't to change anything inside webapp, let say instead of printing "here are weather conditions for 01201" I wanna print "current weather conditions for 01201 are" and I build it from main pom.xml of course, nothing changes .. the old message prints, however if I take webapp outside the parent, change it without building anything than paste it back to the parent, than building it all over again only then will I get changes updated.
Gandalf StormCrow
@romaintaz Should I share project/code? cause I'm now pretty sure how maven works and this shouldn't happen. Thank you
Gandalf StormCrow
What do you mean by "if I take webapp *outside* the parent"? You mean that you remove the webapp from the `<modules>` list of the parent? Maybe you can edit your question and show us your pom.xml files...
romaintaz
@romaintaz, hi romaintaz , I edited my question, its pure book example I didn't modify anything I was just following the book, maybe it has some more errors , I discovered 2 so far, fixed them, this I can but in sort of "hack-ish" way ..
Gandalf StormCrow