Maven管理Spring

【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 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/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>
<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&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;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”文件中的。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章