您的位置:首页 > 编程语言 > Java开发

Maven管理Spring

2017-05-24 17:24 316 查看
【pom.xml配置文件信息】

Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。作用类似ant的build.xml文件,功能更强大。

该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

————————————————————————————————

主要配置信息:

<project>  

    <parent>  

        ...  

    </parent>  

      

    <modelVersion>4.0.0</modelVersion>  

  

    <!-- The Basics -->  

    <groupId>...</groupId>  

    <artifactId>...</artifactId>  

    <version>...</version>  

    <packaging>...</packaging>  

      

    <scm>  

        ...  

    </scm>  

      

    <dependencies>  

        ...  

    </dependencies>  

      

    <dependencyManagement>  

        ...  

    </dependencyManagement>  

      

    <modules>  

        ...  

    </modules>  

      

    <properties>  

        ...  

    </properties>  

  

    <!-- Build Settings -->  

    <build>  

        ...  

    </build>  

    <reporting>  

        ...  

    </reporting>  

  

    <!-- More Project Information -->  

    <name>...</name>  

    <description>...</description>  

    <url>...</url>  

    <inceptionYear>...</inceptionYear>  

      

    <licenses>  

    </licenses>  

      

    <organization>  

    </organization>  

      

    <developers>  

    </developers>  

      

    <contributors>  

    </contributors>  

  

    <!-- Environment Settings -->  

    <issueManagement>  

    </issueManagement>  

      

    <ciManagement>  

    </ciManagement>  

      

    <mailingLists>  

    </mailingLists>  

      

    <prerequisites>  

    </prerequisites>  

      

    <repositories>  

    </repositories>  

      

    <pluginRepositories>  

    </pluginRepositories>  

      

    <distributionManagement>  

    </distributionManagement>  

      

    <profiles>  

    </profiles>  

</project>  

—————————————————————————————————

<project>  

    <parent>  

        ...  <父项目坐标>

    </parent>  

      

    <modelVersion>4.0.0</modelVersion>  <POM版本号>

  

    <!-- The Basics -->  

    <groupId>...</groupId>  

    <artifactId>...</artifactId>  

    <version>...</version>  

    <packaging>...</packaging>  

    
<!--  maven的写作相关属性:

groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par

    
--!>

    <scm>  

        ...  

    </scm>  

      
POM之间的关系:
如依赖关系:依赖关系列表(dependency list)是POM的重要部分

    <dependencies>  

        
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<scope></scope>
</dependency>

    </dependencies>

  

      

    <dependencyManagement>  

        ...  

    </dependencyManagement>  

      

    <modules>  

        ...  

    </modules>  

      

    <properties>  

        ...  

    </properties>  

  

    <!-- Build Settings -->  

    <build>  

        ...  

    </build>  

    <reporting>  

        ...  

    </reporting>  

  

    <!-- More Project Information -->  

    <name>...</name>  

    <description>...</description>  

    <url>...</url>  

    <inceptionYear>...</inceptionYear>  

      

    <licenses>  

    </licenses>  

      

    <organization>  

    </organization>  

      

    <developers>  

    </developers>  

      

    <contributors>  

    </contributors>  

  

    <!-- Environment Settings -->  

    <issueManagement>  

    </issueManagement>  

      

    <ciManagement>  

    </ciManagement>  

      

    <mailingLists>  

    </mailingLists>  

      

    <prerequisites>  

    </prerequisites>  

      

    <repositories>  

    </repositories>  

      

    <pluginRepositories>  

    </pluginRepositories>  

      

    <distributionManagement>  

    </distributionManagement>  

      

    <profiles>  

    </profiles>  

</project>  

—————————————————————————————————

<?xml versio
4000
n="1.0" encoding="UTF-8"?>

<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>my.mycompany</groupId>
<artifactId>myapp</artifactId>
<name>spring_sts_mvc_jdbc</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.2.2.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>

<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</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>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>

<dependency>

    <groupId>org.hsqldb</groupId>

    <artifactId>hsqldb</artifactId>

    <version>2.2.9</version>

    <scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>

</dependency>
</dependencies>

    <build>

        <plugins>

            <plugin>

                <artifactId>maven-eclipse-plugin</artifactId>

                <version>2.9</version>

                <configuration>

                    <additionalProjectnatures>

                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

                    </additionalProjectnatures>

                    <additionalBuildcommands>

                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

                    </additionalBuildcommands>

                    <downloadSources>true</downloadSources>

                    <downloadJavadocs>true</downloadJavadocs>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>2.5.1</version>

                <configuration>

                    <source>1.6</source>

                    <target>1.6</target>

                    <compilerArgument>-Xlint:all</compilerArgument>

                    <showWarnings>true</showWarnings>

                    <showDeprecation>true</showDeprecation>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>exec-maven-plugin</artifactId>

                <version>1.2.1</version>

                <configuration>

                    <mainClass>org.test.int1.Main</mainClass>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>

------------------------------------------------------

springboot 用maven管理的配置:

<?xml version="1.0" encoding="UTF-8"?>

<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>hk.com.vsc.boss</groupId>
<artifactId>boss_invoice_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>boss_invoice_service</name>
<description>boss_invoice_service project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency&
caae
gt;
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>

        <exclusion>

        <groupId>org.apache.tomcat</groupId>

        <artifactId>tomcat-jdbc</artifactId>

        </exclusion>

    </exclusions>
</dependency>

<!-- The Apache PDFBox library is an open source Java tool for working 
with PDF documents. -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.10</version>
</dependency>

<!-- fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>

<dependency>

    <groupId>com.zaxxer</groupId>

    <artifactId>HikariCP</artifactId>
</dependency>

<!-- <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
       <profile>
           <id>dev-mysql</id>
           <properties>
               <environment>dev-mysql</environment>
               <spring.profiles.active>dev,mysql</spring.profiles.active>
               <database.driverClassName>org.mariadb.jdbc.Driver</database.driverClassName>
               <datasource.url>jdbc:mysql://localhost:3306/boss?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
               </datasource.url>
               <datasource.user>development_user</datasource.user>
               <datasource.password>development_password</datasource.password>
           </properties>
<dependencies>
<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>1.5.8</version>
</dependency>
</dependencies>            
       </profile>       
   </profiles>
   
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

<resources>

  <resource>

    <directory>src/main/resources</directory>

    <filtering>true</filtering>

  </resource>
<resource>

    <directory>src/main/resources-${environment}</directory>

    <filtering>true</filtering>

  </resource> 
</resources>
</build>

</project>

【Maven的pom.xml多项目以来配置】

多项目管理

maven的多项目管理也是非常强大的。一般来说,maven要求同一个工程的所有子项目都放置到同一个目录下,每一个子目录代表一个项目,比如

总项目/

pom.xml 总项目的pom配置文件

子项目1/

pom.xml 子项目1的pom文件

子项目2/

pom.xml 子项目2的pom文件

按照这种格式存放,就是继承方式,所有具体子项目的pom.xml都会继承总项目pom的内容,取值为子项目pom内容优先。

要设置继承方式,首先要在总项目的pom中加入如下配置

Xml代码  

<modules>  

    <module>simple-weather</module>  

    <module>simple-webapp</module>  

</modules>  

 

其次在每个子项目中加入

<parent>

  <groupId>org.sonatype.mavenbook.ch06</groupId>

  <artifactId>simple-parent</artifactId>

  <version>1.0</version>

</parent>  

即可。

当然,继承不是唯一的配置文件共用方式,maven还支持引用方式。引用pom的方式更简单,在依赖中加入一个type为pom的依赖即可。

Xml代码  

<project>  

  <description>This is a project requiring JDBC</description>  

  ...  

  <dependencies>  

    ...  

   <dependency>  

      <groupId>org.sonatype.mavenbook</groupId>  

      <artifactId>persistence-deps</artifactId>  

      <version>1.0</version>  

      <type>pom</type>  

    </dependency>  

  </dependencies>  

</project>  

【Maven管理spring、(pom.xml)】使用tree展示原型目录结构:

 


【profile】

    profile可以让我们定义一系列的配置信息,然后指定其激活条件。

这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等。

profile的定义位置:

(1)针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。

(2)针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。

(3)全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息