2010-02-05 71 views
3

我在这里阅读了关于以前的maven问题的一些有用的帖子,我目前对学习maven非常感兴趣(因为我喜欢它,因为我的老板需要我)。我目前正在阅读[这本] [1]一书,我正在通过例子工作。它是一本简单明了的书,但其内部存在一些错误(琐碎的),但对于像我这样的新手来说,很难发现,一旦发现它可以很容易地修复。有没有其他书更好地理解从上到下的maven?问题的关于maven的问题

第二部分是关于一个例子,这本书,也许一个简单的解释就解决了我的怀疑。

这是事情,我做了一个simple-weather项目在Java中检索从雅虎天气服务器的天气条件,给予特定的邮政编码,它返回天气信息。

然后我做了一个'simple-webapp'(带有maven以及上面我忘记提到的那个),这基本上是一个web项目,它有一些默认的servlet已经与maven一起使用,它什么都不做。我想要将这两个项目合并为一个,所以我创建了一个包含2个模块的pom.xml,其中包含1个用于检索信息(java项目)和其他信息以将其显示在Web(Web应用程序)上的信息。

我在结束了一切工作,但这里是奇怪的事情。如果我做的webapp显示任何字符串“name”可以说,然后独立打造它,它不正是打印字符串。但是,当我把webapp放在“父项目”中,并将此字符串更改为“name1”并将其构建为一个partent-project子模块时。没有任何更改..

因此,我回到了这一点,因为简单的webapp是依赖于简单的天气我不能再建立它自己,所以现在,如果我想对webapp做一些更改..修改webapp的“父项目”之外建立它然后粘贴它回来到父项目,然后更改将适用,为什么是这样,为什么我不能直接更改servlet内容/或在webapp中添加另一个作为“父项目”的一部分?

谢谢。我知道它长而枯燥的问题,但我只是想学的东西,有没有更好的地方,要求比这里:d

编辑 - 下面是POM文件每个项目:

1.简单母体的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"> 
    <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.简单全天候的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"> 
<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.简单的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"> 
<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> 
+0

请解释您运行的目标以及从哪里运行。 – 2010-02-05 16:38:54

+0

我正在运行,maven clean,然后从父pom.xml安装maven – 2010-02-08 09:36:43

回答

4

我不知道完全明白你的问题。但是,让我们来解释一下Maven中的一些原则。

所以,你有这样的结构:

parent 
    + simple-weather 
    + simple-webapp 

就某个Maven的点,我们有3个项目在这里:

  • ,这是一个pom项目(即其packaging属性设置为pom
  • 简单天气,这是一个jar项目,并有父母作为父母。
  • 简单的web应用,这是一个war项目,有为家长和简单天气的依赖。

父项目使用两个概念,Maven的:

  • 继承,这不能不说所有他的孩子(简单天气简单的web应用)将继承他所有的属性(这个概念与Java中的extends几乎相同)。
  • 聚合,由定义<modules>定义。聚合意味着将在每个module上运行将在该项目上运行的每个命令。

如果我建立(使用mvn clean install)在目录发生的呢?

  1. Maven将“编译” 项目,然后安装在本地存储库中的pom.xml。
  2. Maven将编译简单天气项目,但由于它有一个父项,Maven会将父项pom.xml文件查看到本地存储库中。一旦JAR被创建,它就被安装在本地存储库中。
  3. Maven将最终编译simple-webapp项目。 Maven将为父pom.xml执行相同的操作,但也适用于简单天气项目。

的situtation在第三点解释是很重要的:如果你想建立的简单的webapp项目,Maven将总是试图找到他所有的依赖关系 - 包括简单全天候 - 从本地(或远程)存储库。

这就是为什么如果你只构造简单的web应用无需编译和安装简单天气,Maven会找不到后一个项目,或将找到的旧版本。因此,总而言之,当您使用Maven处理多模块项目时,尝试始终从根目录(或父目录)运行构建和安装命令。

我希望这个解释很清楚,并且帮助你理解你的情况会发生什么。不要犹豫,要求提供更多信息...

+0

谢谢你的解释,这肯定有助于为我澄清一些事情,但我没有完全得到我的问题的答案。这是事情,如果我不想改变webapp内的任何东西,让我们说,而不是打印“这里是01201的天气条件”我想打印“当前的天气条件01201是”,我从它的主要pom.xml当然,没有任何变化..旧的信息打印,但是如果我把webapp带到父母之外,不用创建任何东西,而是将其粘贴回父母,而不是一遍又一遍地修改,只有这样我才能得到更新。 – 2010-02-05 13:15:26

+0

@romaintaz我应该分享项目/代码吗?因为我现在很确定maven是如何工作的,而这不应该发生。谢谢 – 2010-02-05 13:15:55

+0

你是指“如果我把webapp *放在父母之外”是什么意思?你的意思是你从父母的''列表中删除了webapp?也许你可以编辑你的问题,并告诉我们你的pom.xml文件... – romaintaz 2010-02-05 13:22:02