Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

maven

download->install: install->run:

Download

Maven权威指南zh_CN.pdf

Requirements:

  • JDK: Maven 3.3 requires JDK 1.7 or above to execute - it still allows you to build against 1.3 and other JDK versions by Using Toolchains

  • Disk: Approximately 10MB is required for the Maven installation itself. In addition to that, additional disk space will be used for your local Maven repository.

The size of your local repository will vary depending on usage but expect at least 500MB.

Install

doc zh_CN

  • environment variable
MAVEN_HOME: D:\Maven\apache-maven-3.3.9
  • path
%MAVEN_HOME%/bin;
  • test:
Mvn -v
  • result
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:4
7+08:00)
Maven home: D:\Maven\apache-maven-3.3.9\bin\..
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: D:\Program Files\Java\jdk1.7.0_79\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

  1. Download the maven apache-maven-3.3.9-bin.tar.gz, unzip and rename the file to maven3.3.9.

  2. use mac terminal , enter

$ pwd

to get current path. enter

$ cd /

$ cd /usr/local

$ ls

can get the dirs under the local package. Then create file maven if not exists.

$ sudo mkdir maven

copy the maven3.3.9 into this path.

$ sudo cp -R /Users/houbinbin/IT/learn/maven/maven3.3.9 /usr/local/maven

3.set the path

$ vi ~/.bash_profile

This is vi editor. First in, you are in , enter i, change to to input words.

M3_HOME=/usr/local/maven/maven3.3.9

PATH=$M3_HOME/bin:$PATH



export M3_HOME

export PATH

then, press Esc change to , enter :wq to save and quit.

exit the mac terminal, and restart the terminal.

echo $M3_HOME
echo $PATH

can see what you set, and

$ mvn -version

like this

houbinbindeMacBook-Pro:~ houbinbin$ mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven/maven3.3.9
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.3", arch: "x86_64", family: "mac"

settings.xml

  • open the settings.xml in %MAVEN_HOME%\conf\settings.xml

you will find code like this…

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository

  <localRepository>/path/to/local/repo</localRepository>
-->

You can change the path any you want;

Create maven

you can create maven project in idea like this:

File-> new Module -> Maven
  • STEP 1: select one from archetype

maven

  • STEP 2: add GroupId,ArtifactId,Version

GroupId

  • STEP 3: set Properties

Properties

you can add code as following if maven build is very slow.

archetypeCatalog=internal

Run project

  • elegant way in cmd
mvn clean install

mvn tomcat7:run
  • common way

tomcat zh_CN

Tips

You can config the maven setting use

crtl+alt+s

then search maven

maven setting

-Dmaven.multiModuleProjectDirectory system propery is not set
  1. you can use Low version maven to fit your idea.

  2. or solve it like this.

-Dmaven.multiModuleProjectDirectory=$M2_HOME

M2_HOME D:\Maven\apache-maven-3.3.9

M2_HOME

pom.xml

  • the default content of pox.xml may like this:
<?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>com.ryo</groupId>
    <artifactId>maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>...</dependency>
        ...
    </dependencies>

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

        <!--maven only compile resources files under package ```resources```, you can solve it.-->

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
  • define the method of package.
<packaging>war</packaging>
  • define the sourceEncoding
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  • define the maven-compiler-plugin
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>
  • define the tomcat7 plugin
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <port>8080</port>
        <path>/</path>
        <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
    </configuration>
</plugin>
  • If you want to skip tests by default but want the ability to re-enable tests from the command line, you need to go via a properties section in the pom:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <skipTests>true</skipTests>
    </configuration>
</plugin>
  • you can use maven shade plugin make class into jar.

scope

  • compile,缺省值,适用于所有阶段,会随着项目一起发布。
  • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
  • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
  • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
  • system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
  • import 它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置

Maven Archetype

archetype

  • what is ?

In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.

  • using

To create a new project based on an Archetype, you need to call mvn archetype:generate goal.

title: Your own archetype create->add: Define project template add->remove: Remove if not need

Create archetype

define archetype

  • create
$ mvn archetype:create-from-project
houbinbindeMacBook-Pro:archetype houbinbin$ mvn archetype:create-from-project
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building archetype 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:create-from-project (default-cli) > generate-sources @ archetype >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:create-from-project (default-cli) < generate-sources @ archetype <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:create-from-project (default-cli) @ archetype ---
[INFO] Setting default groupId: com.ryo
[INFO] Setting default artifactId: archetype
[INFO] Setting default version: 1.0.0
[INFO] Setting default package: com.ryo
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building archetype-archetype 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ archetype-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 10 resources
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ archetype-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-archetype-plugin:2.4:jar (default-jar) @ archetype-archetype ---
[INFO] Building archetype jar: /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/target/archetype-archetype-1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.835 s
[INFO] Finished at: 2016-06-11T21:57:36+08:00
[INFO] Final Memory: 14M/309M
[INFO] ------------------------------------------------------------------------
[INFO] Archetype project created in /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.955 s
[INFO] Finished at: 2016-06-11T21:57:36+08:00
[INFO] Final Memory: 14M/245M
[INFO] ------------------------------------------------------------------------
  • install
$ cd target/generated-sources/archetype/
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building archetype-archetype 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ archetype-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 10 resources
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ archetype-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-archetype-plugin:2.4:jar (default-jar) @ archetype-archetype ---
[INFO] Building archetype jar: /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/target/archetype-archetype-1.0.0
[INFO]
[INFO] --- maven-archetype-plugin:2.4:integration-test (default-integration-test) @ archetype-archetype ---
[INFO] Processing Archetype IT project: basic
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: archetype-archetype:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: packageInPathFormat, Value: it/pkg
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[WARNING] Don't override file /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic/.idea/copyright/profiles_settings.xml
[INFO] project created from Archetype in dir: /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ archetype-archetype ---
[INFO] Installing /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/target/archetype-archetype-1.0.0.jar to /Users/houbinbin/.m2/repository/com/ryo/archetype-archetype/1.0.0/archetype-archetype-1.0.0.jar
[INFO] Installing /Users/houbinbin/IT/code/branches/archetype/target/generated-sources/archetype/pom.xml to /Users/houbinbin/.m2/repository/com/ryo/archetype-archetype/1.0.0/archetype-archetype-1.0.0.pom
[INFO]
[INFO] --- maven-archetype-plugin:2.4:update-local-catalog (default-update-local-catalog) @ archetype-archetype ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.885 s
[INFO] Finished at: 2016-06-11T22:00:07+08:00
[INFO] Final Memory: 15M/309M
[INFO] ------------------------------------------------------------------------

清楚不需要的项目原型

  • maven 本地的项目项目原型默认存放在:
~/.m2/repository/archetype-catalog.xml

你可以手动修改此文件

  • deploy

add these in the pom.xml of archetype

<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Nexus Releases Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Nexus Snapshots Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

then

$   mvn deploy
  • use
$ mkdir /tmp/archetype
$ cd /tmp/archetype
$ mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> com.ryo:archetype-archetype (archetype-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': : com.ryo
Define value for property 'artifactId': : test
Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0
Define value for property 'package':  com.ryo: : com.ryo
Confirm properties configuration:
groupId: com.ryo
artifactId: test
version: 1.0.0
package: com.ryo
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: archetype-archetype:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.ryo
[INFO] Parameter: artifactId, Value: test
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: package, Value: com.ryo
[INFO] Parameter: packageInPathFormat, Value: com/ryo
[INFO] Parameter: package, Value: com.ryo
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: groupId, Value: com.ryo
[INFO] Parameter: artifactId, Value: test
[WARNING] Don't override file /Users/houbinbin/IT/code/branches/test/test/.idea/copyright/profiles_settings.xml
[INFO] project created from Archetype in dir: /Users/houbinbin/IT/code/branches/test/test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:34 min
[INFO] Finished at: 2016-06-11T22:11:32+08:00
[INFO] Final Memory: 14M/309M
[INFO] ------------------------------------------------------------------------

Add archetype

add archetype

Input the location of your archetype project, then you can use it just like other maven archetype.

Remove archetype

~/Library/Caches/IntelliJIdea<version>/Maven/Indices/UserArchetypes.xml

Open and edit this file, then restart IntelliJIdea.

项目原型简介

  • 生成项目架构
houbinbindeMacBook-Pro:archetype-resources houbinbin$ ls
README.md                       __rootArtifactId__-dal          __rootArtifactId__-surface      __rootArtifactId__-util         app-demo.iml
__rootArtifactId__-biz          __rootArtifactId__-service      __rootArtifactId__-test         __rootArtifactId__-web          pom.xml
houbinbindeMacBook-Pro:archetype-resources houbinbin$ pwd
/Users/houbinbin/IT/code/app-demo/target/generated-sources/archetype/target/classes/archetype-resources
  • 元数据指定 (archetype-metadata.xml)
houbinbindeMacBook-Pro:maven houbinbin$ ls
archetype-metadata.xml
houbinbindeMacBook-Pro:maven houbinbin$ pwd
/Users/houbinbin/IT/code/app-demo/target/generated-sources/archetype/target/classes/META-INF/maven

实例:

archetype-metadata.xml

Multi-module

Create

Create an empty maven project, it’s pom.xml like this:

<?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>com.ryo</groupId>
    <artifactId>multiModule</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

Create child module util for multiModule like this, also we create another module dao in this way:

child-module

  • The pom.xml of multiModule will be:
<?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>com.ryo</groupId>
    <artifactId>multiModule</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>util</module>
    </modules>

</project>
  • The pom.xml of util like this:
<?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">
    <parent>
        <artifactId>multiModule</artifactId>
        <groupId>com.ryo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>util</artifactId>

</project>
  • StringUtil.java in util module
public class StringUtil {
    private static final String EMPTY_STRING = "";

    private StringUtil(){}

    public static boolean isEmpty(String string) {
        return string == null || string.trim().equals(EMPTY_STRING);
    }
}

Use

If we want to use StringUtil.java of util in dao module, we should do it like this:

  • install

Install the module you want to use in module util or multiModule(root module).

multi-module

  • define

Define the dependency of util in dao module’s pom.xml

<?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">
    <parent>
        <artifactId>multiModule</artifactId>
        <groupId>com.ryo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.ryo</groupId>
            <artifactId>util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • use
public class UserDao {
    public boolean login(String username, String password) {
        return StringUtil.isEmpty(username) || StringUtil.isEmpty(password);
    }
}

Tips

If you define the dependency of [one module] in root module, all children of it can use the [one module]. But, usually we use it may like this:

  • pom.xml of root module, declare the use.
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.ryo</groupId>
            <artifactId>util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>
  • pom.xml of dao module, define the use.
<dependencies>
    <dependency>
        <groupId>com.ryo</groupId>
        <artifactId>util</artifactId>
    </dependency>
</dependencies>

Tips

  • Get snapshot jar immediately

When you use Idea to get the snapshot jar, you may not get it immediately. Here is the solve way:

+, to open the Preference, search for maven, and choose the Always update snapshots

  • messy code(乱码)

mvn run messy code

add this under properties in pom.xml

<properties>
    <argLine>-Dfile.encoding=UTF-8</argLine>
</properties>
  • 下载很慢的情况

blog zh_CN

~/.m2/setting.xml 中添加:

<mirrors>
       <!-- 阿里云仓库 -->
      <mirror>
           <id>alimaven</id>
           <mirrorOf>central</mirrorOf>
           <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
       </mirror>
</mirrors>

项目依赖

有时候依赖其他的三方jar包较多,有些jar被重复引入且版本不一致。(比如slf4j-api.jar)

可以在某一个项目下使用 mvn dependency:tree

D:\CODE\other\framework\framework-cache>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building framework :: Module :: Cache 1.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ framework-cache ---
[INFO] com.framework:framework-cache:jar:1.0.2-SNAPSHOT
[INFO] +- com.framework:framework-tool:jar:1.0.2-SNAPSHOT:compile
[INFO] |  +- com.framework:framework-common:jar:1.0.2-SNAPSHOT:compile
[INFO] |  |  +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  |  \- org.projectlombok:lombok:jar:1.16.8:compile
[INFO] |  +- org.apache.logging.log4j:log4j-api:jar:2.5:compile
[INFO] |  +- org.apache.logging.log4j:log4j-core:jar:2.5:compile
[INFO] |  +- com.alibaba:fastjson:jar:1.2.8:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.0:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.0:compile
[INFO] |  +- org.reflections:reflections:jar:0.9.10:compile
[INFO] |  |  +- com.google.guava:guava:jar:15.0:compile
[INFO] |  |  +- org.javassist:javassist:jar:3.20.0-GA:compile
[INFO] |  |  \- com.google.code.findbugs:annotations:jar:2.0.1:compile
[INFO] |  \- junit:junit:jar:4.12:compile
[INFO] |     \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- org.springframework:spring-context:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.3.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.2.3.RELEASE:compile
[INFO] +- org.springframework.data:spring-data-redis:jar:1.3.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] |  \- org.springframework:spring-tx:jar:4.2.3.RELEASE:compile
[INFO] +- redis.clients:jedis:jar:2.4.2:compile
[INFO] |  \- org.apache.commons:commons-pool2:jar:2.0:compile
[INFO] +- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.5:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.8.5:compile
[INFO] \- commons-net:commons-net:jar:3.5:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.927 s
[INFO] Finished at: 2017-01-24T10:57:41+08:00
[INFO] Final Memory: 15M/304M
[INFO] ------------------------------------------------------------------------
  • 排除依赖

exclusion

Import Local Jars

场景:有些 jar 文件中央仓库没有。在 maven 项目中使用我们就需要采取一些技巧。

导入到本地仓库

  • 导入到 nexus 仓库

  • 导入到 maven 仓库

maven添加sqlserver的jdbc驱动包

一、 在有 sqljdbc4.jar 的文件下

mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0

命令解释:mvn install:install-file -Dfile=”jar包的绝对路径” -Dpackaging=”文件打包方式” -DgroupId=groupid名 -DartifactId=artifactId名 -Dversion=jar版本

二、引入

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
</dependency>

指定绝对路径

Maven中使用本地JAR包

  1. 路径指定时使用 /, 为了跨平台。

  2. 如果是 maven 多模块项目。可以使用类似如下的 jar 路径指定方式:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/../lib/sqljdbc4.jar</systemPath>
</dependency>

通过编译参数

既使用maven编译,又使用lib下的Jar包