Maven 2项目 [英] Maven 2 Projects

查看:76
本文介绍了Maven 2项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个项目


  1. 执行CRUD操作的服务器端项目。 (让我们说P1)

  2. 客户端JSF表示层java web项目。 (P2)

由于P2将取决于P1中的所有实体,因此我将P1包含在P2的构建路径中。 (不是通过maven)

Since P2 would depend upon all the entities in P1, I have included P1 in the build path of P2. (Not through maven)

每当我对P1进行更改时,我必须导出一个jar并将其放在Tomcat webapp文件夹中,否则我会抛出NoClassDef。

Everytime I make changes to P1, I have to export a jar and place it in Tomcat webapp folder, else I would throw NoClassDef.

这可以自动化吗?

推荐答案

你可以建立 main Maven项目有三个子模块,比如 master platform parent

You can build a main Maven project which has three submodules, say master, platform and parent.

(注意:对于以下所有项目,您应该根据应用程序架构设置GAV参数,以便具有重要名称易于理解)

(Note: for all the following projects you should set GAV parameters consistently with your application architecture, in order to have significant names easy to understand)

main 项目只是评估其他项目的顺序作者:Maven:

The main project has simply the order in which the other projects will be evaluated by Maven:

<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.acme.projects</groupId>
  <artifactId>main</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
        <plugins>
            <plugin>
                 ....
            </plugin>
        </plugins>
    </build>

<modules>
    <module>master</module>
    <module>parent</module>
    <module>platform</module>
</modules>

master pom包含要构建的项目列表及其订单(又名Reactor订单)

The master pom contains the list of project to be built and their order (aka Reactor order)

<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.acme.projects</groupId>
      <artifactId>master</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>

    <modules>
        <module>../../P1</module>
        <module>../../P2</module>       
    </modules>
</project>

正如您所见,P2必须在P1之后插入,因为P2具有P1作为依赖。

As you can see P2 must be inserted after P1, since P2 has P1 as a dependency.

平台 pom包含有关您平台的所有信息,如JDK版本,maven插件版本,编码等。

The platform pom contains all information about your platform, like JDK version, maven plugin versions, encoding and so on.

<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.acme.projects</groupId>
    <artifactId>platform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.3.1</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jarsigner-plugin</artifactId>
                    <version>1.2</version>
                </plugin>
                <plugin>
                .....
                </plugin>
            </plugins> 
        </pluginManagement>
    </build>

</project>

pom的平台为pom父项,包含有关您将在项目中使用的依赖项的所有全局GAV信息(Spring,CXF,junit,log4j等)。

The parent pom has the platform pom as a parent and contains all global GAV information about the dependencies you are going to use in your project (Spring, CXF, junit, log4j etc.)

<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>
    <parent>
        <groupId>com.acme.projects</groupId>
        <artifactId>platform</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../platform</relativePath>
    </parent>

    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <groupId>com.acme.projects</groupId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    
        <spring.version>4.1.6.RELEASE</spring.version>
        <!-- == Web Services == -->
        <cxf.version>3.0.4</cxf.version>
        <!-- == Testing == -->
        <junit.version>4.12</junit.version>
    </properties>

    <dependencyManagement>
        <dependencies>

            <!-- == LOGGING == -->
            <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

            <!-- == SPRING == -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</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-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>


            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

最后你将有一个这样的项目结构:

Finally you will have a project structure like this:

你会必须将正确的pom放在每个项目文件夹中。
现在,P1和P2项目都必须将 parent 项目作为父项,而且P2必须具有P1的依赖项。

and you will have to put the right pom inside each project folder. Now, both P1 and P2 projects must have parent project as parent, and moreover P2 must have a dependency to P1.

为了构建整个反应器,你只需将自己设置到 main 目录并使用命令

In order to have the whole reactor built, you have only to set yourself into the main directory and use the command

mvn clean install

编译你的P1项目并将其作为P2项目的依赖项。

which will compile your P1 project and make it available as a dependency for your P2 project.

这种方法有很多优点:


  1. 你可以在列表中添加更多项目并让它们自动编译而不必担心放置jar(每个新项目都必须有 parent 作为父项)

  2. 使用Jenkins之类的CI工具更轻松地自动化流程,以便控制编译过程

  3. 在层次结构顶部集中依赖项版本并根据需要专门研究子项目

  1. you can add more projects to your list and have them automatically compiled without worrying about placing jars around (every new project must have parent as parent)
  2. make easier to automate the process with CI tools like Jenkins, to have compiling process under control
  3. concentrate dependencies version at the top of your hierarchy and specialize in a subproject as needed

这篇关于Maven 2项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆