3

我在这里阅读了一些关于以前 maven 问题的有用帖子,我目前对学习 maven 非常感兴趣(因为我喜欢它,因为我的老板要求我这样做)。我目前正在阅读 [this][1] 书,并且正在通过示例进行工作。这是一本简单的书,但里面有一些错误(微不足道的错误),但是对于像我这样的新手来说很难发现,一旦发现它就可以轻松修复。有没有其他书更好地从上到下理解maven?

问题的第二部分是与本书中的一个例子有关,也许一个简单的解释可以解决我的疑问。

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

然后我做了一个'simple-webapp'(使用maven以及上面我忘了提到的那个),它基本上是一个web项目,它已经有一些默认的servlet和maven,它什么都不做。

我有一些parent-project我想将这两个项目合并为一个,所以我制作了一个 pom.xml,它有 2 个模块,1 个用于检索信息(java 项目),另一个用于在网络上显示它(网络应用程序)。

我最终使一切正常,但奇怪的是..如果我让 webapp 显示任何字符串“名称”,让我们说然后独立构建它,它确实会打印该字符串。但是,当我将 webapp 放入“父项目”并将此字符串更改为“name1”并将其构建为 sa partent-project 子模块时..没有任何变化..

所以我回到正题,因为 simple-webapp 依赖于 simple-weather 我不能再单独构建它,所以现在如果我想对 webapp 进行一些更改.. 在“parent-”之外修改 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. 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">
<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>
4

1 回答 1

4

我不确定完全理解你的问题。但是,让我们解释一下Maven中的一些原则。

所以你有这样一个结构:

parent
  + simple-weather
  + simple-webapp

从 Maven 的角度来看,我们这里有 3 个项目:

  • parent,这是一个pom项目(即其packaging属性设置为pom
  • simple-weather,这是一个jar项目,其父级为父级。
  • simple-webapp是一个war项目,其parent为 parent,simple-weather为依赖项。

父项目在 Maven 中使用了两个概念:

  • 继承,表示他的所有孩子(simple-weathersimple-webapp)将继承他的所有属性(这个概念与 Java 中的几乎相同extends)。
  • 聚合,由 的定义定义<modules>。聚合意味着将在项目上运行的每个命令也将在每个module.

如果我在目录上构建(使用mvn clean install)会发生什么?

  1. Maven 将“编译”项目,然后将 pom.xml安装在本地存储库中。
  2. Maven 将编译simple-weather项目,但由于它有父级,Maven 会将父级 pom.xml 文件查找到本地存储库中。创建 JAR 后,它会安装在本地存储库中。
  3. Maven 最终将编译simple-webapp项目。Maven 将为父 pom.xml 做同样的事情,但也适用于simple-weather项目。

第 3 点中解释的情况很重要:如果您想构建simple-webapp项目,Maven 将始终尝试从本地(或远程)存储库中找到他的所有依赖项 - 包括simple-weather 。

这就是为什么如果你只构建simple-webapp而不构建和安装simple-weather,Maven 将找不到后一个项目,或者会找到旧版本。

总而言之,当您使用 Maven 处理多模块项目时,请尝试始终从根(或父)目录运行构建和安装命令。

我希望这个解释足够清楚,并帮助您了解您的情况。不要犹豫,询问更多信息......

于 2010-02-05T13:04:56.330 回答