【问题标题】:Question about maven关于maven的问题
【发布时间】:2010-02-05 12:50:02
【问题描述】:

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

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

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

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

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

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

所以我回到正题,因为 simple-webapp 依赖于 simple-weather,我无法再单独构建它,所以现在如果我想对 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. 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>

【问题讨论】:

  • 请说明您的目标是什么以及从哪里开始。
  • 我正在运行,maven clean 然后从父 pom.xml 安装 maven

标签: java maven-2 maven


【解决方案1】:

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

所以你有这样的结构:

parent
  + simple-weather
  + simple-webapp

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

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

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

  • 继承,表示他的所有孩子(simple-weathersimple-webapp)将继承他的所有属性(这个概念几乎是同一回事正如 Java 中的 extends)。
  • 聚合,由&lt;modules&gt; 的定义定义。聚合意味着将在项目上运行的每个命令也将在每个module 上运行。

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

  1. Maven 将“编译”parent 项目,然后在本地存储库中安装 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 处理多模块项目时,请尝试始终从根(或父)目录运行构建和安装命令。

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

【讨论】:

  • 感谢您的解释,这确实帮助我澄清了一些事情,但我并没有完全得到我的问题的答案。事情是这样的,如果我不想在 webapp 中更改任何内容,可以说不是打印“这里是 01201 的天气状况”,而是要打印“01201 的当前天气状况”,我从 main pom.xml 构建它当然,没有任何改变..旧消息打印出来,但是如果我将 webapp 放在父级之外,则在不构建任何内容的情况下对其进行更改,而不是将其粘贴回父级,而不是重新构建它,然后我才会更新更改。
  • @romaintaz 我应该分享项目/代码吗?因为我现在很确定 Maven 是如何工作的,这不应该发生。谢谢
  • “如果我将 webapp 放在父级之外”是什么意思?您的意思是从父级的&lt;modules&gt; 列表中删除webapp?也许您可以编辑您的问题并向我们展示您的 pom.xml 文件...
  • 嗨 romaintaz ,我编辑了我的问题,它的纯书示例我没有修改任何内容我只是按照这本书,也许它还有一些错误,我到目前为止发现了 2 个,修复了它们,这个我可以,但以某种“hack-ish”的方式..
猜你喜欢
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多