idea 一个多maven项目聚合

今天突然下回顾以下Maven的聚合项目搭建,因为我们平时大多都是公司已经搭好的项目,平时自己也知道原理,但有时不去实践一下,你根本不知道,你是否会。

我有时候会比较膨胀,觉得这也没什么跟着网上文章搭建一下就好了,应该就能成功,没必要去搭建,但经历告诉我,实践出真知,比空头下要好多了


搭建的时候没遇到多少问题,唯一碰到的问题就是

[ERROR] Failed to execute goal on project Mavens-service: Could not resolve dependencies for project com.wm:Mavens-service:jar:1.0-SNAPSHOT: Failed to collect dependencies at com.wm:Mavens-core:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for com.wm:Mavens-core:jar:1.0-SNAPSHOT: Could not find artifact com.wm:Mavens-parent:pom:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException


先声明一下,我这个项目构建是这样的:
主:Mavens-parent

子:Mavens-core,Mavens-service,Mavens-web


这里Mavens-service依赖Mavens-core,Mavens-core 我已经install 进仓库了,为什么还会报这样的错误呢

通过查找,最终是找到了解决方法,是因为我还没有安装Mavens-parent导致,安装完Mavens-parent后在进行Mavens-service的安装就没有问题了。



在此,我顺便记录一下几个pom文件

Mavens-parent:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.wm</groupId>
  <artifactId>Mavens-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Mavens-web</module>
        <module>Mavens-core</module>
      <module>Mavens-service</module>
    </modules>
    <packaging>pom</packaging>

  <name>Mavens-parent</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>4.11</junit.version>
    <spring.version>4.1.7.RELEASE</spring.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- hibernate4 -->
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.5.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.5.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.2.5.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
      </dependency>
      <dependency>
        <groupId>com.wm</groupId>
        <artifactId>Mavens-service</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>jar</type>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>com.wm</groupId>
        <artifactId>Mavens-core</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>jar</type>
        <scope>compile</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5.0-1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>
</project>



Mavens-core:

<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Mavens-parent</artifactId>
        <groupId>com.wm</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>Mavens-core</artifactId>
    <packaging>jar</packaging>
    <name>Mavens-core</name>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
        </dependency>
    </dependencies>
</project>

Mavens-service

<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Mavens-parent</artifactId>
        <groupId>com.wm</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Mavens-service</artifactId>
    <packaging>jar</packaging>

    <name>Mavens-service</name>


    <dependencies>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>Mavens-core</artifactId>
        </dependency>
    </dependencies>
</project>

Mavens-web

<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">
    <parent>
        <artifactId>Mavens-parent</artifactId>
        <groupId>com.wm</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>Mavens-web</artifactId>
    <packaging>war</packaging>
    <name>Mavens-web Maven Webapp</name>
    <dependencies>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>Mavens-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>Mavens-service</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>Mavens-web</finalName>
        <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.8.v20160314</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                        <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
                    </webAppConfig>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


这里介绍一下父类里的dependencyManagement 是子类可以继承,但要在需要用到里面包的子类,引入这个包,但是不需要填写version或其他配置,父类都配置好了。

如:

 <dependency>
            <groupId>com.wm</groupId>
            <artifactId>Mavens-core</artifactId>
        </dependency>

父类里直接写在dependencys里的子类都会继承下来

猜你喜欢

转载自blog.csdn.net/xiao__miao/article/details/73551039