3

私は以前のmavenの質問についてSOでいくつかの有用な投稿を読みました.私は現在mavenを学ぶことに非常に興味があります(私はそれが好きで、上司が私に要求しているため). 私は現在[これ][1]の本を読んでいて、例を通して自分のやり方で取り組んでいます. それは簡単な本ですが、内部にいくつかのエラー(些細なもの)がありますが、私のような初心者にとっては見つけるのが難しい場合があります。見つけたら簡単に修正できます. Mavenを上から下まで理解するのに適した本は他にありますか?

質問の2番目の部分は、この本の例に関連しています。簡単な説明で私の疑問が解決するかもしれません。

simple-weather気象情報を返す特定の郵便番号を指定して、yahoo 気象サーバーから気象条件を取得するプロジェクトを Java で作成しました。

次に、「simple-webapp」を作成しました(上記のものと同様にmavenを使用して、言及するのを忘れていました)。これは基本的に、mavenを使用してデフォルトのサーブレットが既に存在するWebプロジェクトであり、何もしません。

そして、parent-projectこれら2つのプロジェクトを1つにマージしたいものがあるので、2つのモジュールを持つpom.xmlを作成しました。

最後にすべてを機能させましたが、ここに奇妙なことがあります.. webappに任意の文字列「名前」を表示させてから、それを個別にビルドすると、その文字列が正確に出力されます。しかし、webapp を「parent-project」に入れ、この文字列を「name1」に変更し、それを sa partent-project サブモジュールとしてビルドすると、何も変わりません..

要点に戻ります。simple-webapp は simple-weather に依存しているため、それを単独で構築することはできません。そのため、webapp に変更を加えたい場合は、「parent-プロジェクト」をそこにビルドし、それを親プロジェクトに貼り付けると、変更が適用されます。なぜ、サーブレットのコンテンツを直接変更できないのか、「親プロジェクト」の一部として Web アプリケーションに別のコンテンツを追加できないのですか。 -事業"?

ありがとう.. 長くてつまらない質問だとは思いますが、私は物事を学ぼうとしているだけで、ここより質問するのに適した場所はありません :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>
4

1 に答える 1

4

あなたの質問を完全に理解することはできません。ただし、Mavenのいくつかの原則について説明しましょう。

したがって、このような構造があります。

parent
  + simple-weather
  + simple-webapp

Mavenの観点から、ここには3つのプロジェクトがあります。

  • pomプロジェクトであるpackaging(つまり、その属性がに設定されているpom
  • simple-weather。これはjarプロジェクトであり、が親になります。
  • warプロジェクトであるsimple-webappは、親として親を持ち、依存関係としてsimple -weatherを持ちます。

親プロジェクトは、Mavenで2つの概念を使用します。

  • 継承。これは、彼のすべての子(simple-weatherおよびsimple-webappextends )がすべてのプロパティを継承することを意味します(この概念は、Javaの場合とほぼ同じです)。
  • の定義によって定義される集約<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-weatherをビルドしてインストールせずにsimple-webappのみをビルドすると、Mavenは後者のプロジェクトを検出しないか、古いバージョンを検出します。

要約すると、Mavenを使用してマルチモジュールプロジェクトで作業する場合は、常にルート(または親)ディレクトリからビルドコマンドとインストールコマンドを実行するようにしてください。

この説明が十分に明確であり、あなたのケースで何が起こっているのかを理解するのに役立つことを願っています。詳細については、遠慮なくお問い合わせください...

于 2010-02-05T13:04:56.330 に答える