我在 SO 上阅读了一些关于以前的 maven 问题的有用帖子,我目前对学习 maven 非常感兴趣(因为我喜欢它并且因为我的老板要求我这样做)。我目前正在阅读 [this][1] 书,并且正在研究示例。这是一本简单的书,但里面有一些错误(小错误),但是对于像我这样的新手来说很难发现,一旦发现就很容易修复。从上到下理解maven还有什么书比较好?
问题的第二部分是关于这本书中的一个例子,也许一个简单的解释可以解决我的疑惑。
事情是这样的,我在 java 中创建了一个 simple-weather
项目,它从雅虎天气服务器检索天气状况,给定特定的邮政编码,它返回天气信息。
然后我制作了一个“simple-webapp”(使用 maven 以及我忘记提及的上面的那个),它基本上是一个 web 项目,其中已经有一些默认的 servlet 和 maven 但它什么都不做。
我有一些 parent-project
我想将这两个项目合并为一个,所以我制作了一个 pom.xml,它有 2 个模块,1 个用于检索信息(java 项目),另一个用于显示它在网络上(网络应用程序)。
最后我让一切正常,但奇怪的是..如果我让 webapp 显示任何字符串“name”让我们说然后独立构建它,它会准确地打印那个字符串。但是当我把 webapp 放在“parent-project”中并将这个字符串更改为“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 中的一些原理。
所以你有这样一个结构:
parent
+ simple-weather
+ simple-webapp
从 Maven 的角度来看,我们这里有 3 个项目:
- parent,这是一个
pom
项目(即其packaging
属性设置为pom
) - 简单天气,这是一个
jar
项目并将父级作为父级。 - simple-webapp,这是一个
war
项目,将 parent 作为父级,将 simple-weather 作为依赖项。
父项目在 Maven 中使用了两个概念:
- 继承,就是说他所有的 child (simple-weather 和simple-webapp)都会继承他所有的属性(这个概念几乎是一回事如 Java 中的
extends
。 - 聚合,由
<modules>
的定义定义.聚合意味着将在项目上运行的每个命令也将在每个module
上运行。 .
如果我在 parent 目录上构建(使用 mvn clean install
)会怎样?
- Maven 将“编译”父 项目,然后在本地存储库中安装 pom.xml。
- Maven 将编译 simple-weather 项目,但由于它有一个父项目,Maven 将在本地存储库中查找父 pom.xml 文件。创建 JAR 后,它会安装在本地存储库中。
- Maven 最终将编译simple-webapp 项目。 Maven 将对父 pom.xml 和 simple-weather 项目执行相同的操作。
第 3 点中解释的情况很重要:如果你想构建 simple-webapp 项目,Maven 将总是尝试找到他的所有依赖项 - 包括simple-weather - 来自本地(或远程)存储库。
这就是为什么如果您只构建 simple-webapp 而没有构建和安装 simple-weather,Maven 将找不到后一个项目,或者会找到一个旧版本。
总而言之,当您使用 Maven 处理多模块项目时,请尝试始终从根(或父)目录运行构建和安装命令。
我希望这个解释足够清楚,可以帮助您理解您的案例中发生的事情。不要犹豫,询问更多信息...
关于java - 关于maven的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2207244/