Maven

Apache Maven

POM项目对象模型

Apache Maven 是一种创新的软件项目管理工具,提供了一个项目对象模型(POM)文件的新概念来管理项目的构建,相关性和文档。
最强大的功能就是能够自动下载项目依赖库。

资源库

本地资源库

默认的文件夹是 “.m2” 目录

settings.xml中localRepository手动指定。
* 添加jar到本地资源库

mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar

中央存储库

所有项目依赖库默认位置

settings.xml中 servers+mirrors

安装和配置

  • JAVA_HOME/MAVEN_HOME/M2_HOME/Path
  • settings.xml

命令

  • 使用模板创建web项目 mvn archetype:generate -DgroupId=com.boot -DartifactId=CounterWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  • mvn post-clean
  • mvn compile
  • mvn test
  • mvn package
  • mvn site

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

    <groupId>org.springboot</groupId>
    <artifactId>springboot</artifactId>
    <name>boot</name>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <!-- 继承于父pom -->
    <parent>
        <groupId>com.boot</groupId>
        <artifactId>boot-pom</artifactId>
        <version>0.0.1</version>
        <relativePath>../boot-pom/pom.xml</relativePath>
    </parent>

    <properties>
        <fastjson.version>1.2.30</fastjson.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <!-- 如果version被忽略,则默认最新版本 -->
                <version>${fastjson.version}</version>
                <!-- 排除依赖 -->
                <exclusions>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <!-- 依赖范围,compile\provided(只在编译和测试时运行)\runtime\test\system\import -->
                <scope>test</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <!-- 声明特定仓库,首先找本地仓库,不得,再找中央仓库,不得,再找java.net仓库 -->
    <repositories>
        <repository>
            <id>java.net</id>
            <url>https://maven.java.net/content/repositories/public/</url>
        </repository>
    </repositories>

    <!-- 不同的环境配置,在项目-properties-maven设置 -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <filters>
                    <filter>../${project.parent.artifactId}/vars/dev.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <filters>
                    <filter>../${project.parent.artifactId}/vars/prod.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>

    <build>
        <finalName>boot</finalName>
        <!-- 指定资源路径 -->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <!-- 编译器插件,指定jdk版本 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3.3</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <!-- 构建自动化,当本项目有变化时,自动构建依赖于本项目的ui和desktop-ui-->
                <plugin>
                    <artifactId>maven-invoker-plugin</artifactId>
                    <version>1.6</version>
                    <configuration>
                        <debug>true</debug>
                        <pomIncludes>
                            <pomInclude>app-web-ui/pom.xml</pomInclude>
                            <pomInclude>app-desktop-ui/pom.xml</pomInclude>
                        </pomIncludes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>build</id>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                     <version>9.3.10.v20160621</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <!-- 聚合 -->
    <modules>
        <module>../boot-intf</module>
        <module>../boot-base</module>
        <module>../boot-core</module>
        <module>../boot-web</module>
        <module>../boot-admin</module>
    </modules>

    <distributionManagement>
        <!--部署项目产生的构件到远程仓库需要的信息-->
        <repository>
        <!--是分配给快照一个唯一的版本号(由时间戳和构建流水号)?还是每次都使用相同的版本号?参见repositories/repository元素-->
            <uniqueVersion/>
            <id>banseon-maven2</id>
            <name>banseon maven2</name>
            <url>file://${basedir}/target/deploy</url>
            <layout/>
        </repository>

        <!--构建的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素-->
        <snapshotRepository>
            <id>banseon-maven2</id>
            <name>Banseon-maven2 Snapshot Repository</name>
            <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url>
        </snapshotRepository>
    </distributionManagement>

</project>

依赖冲突

多个jar同时应用了相同的jar时,产生依赖冲突,maven的策略是:
* 短路优先
* 声明优先

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值