项目管理工具Maven仓库配置详解

Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。

  • 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.alanlee</groupId>
  <artifactId>UidpWeb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <!-- maven属性的使用 -->
  <properties>
      <plugin.version>2.5</plugin.version>
  </properties>

  <!-- 依赖配置的使用 -->
  <dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <!-- 测试范围有效,在编译和打包时都不会使用这个依赖 -->
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <!-- 在编译和测试的过程有效,最后生成war包时不会加入 -->
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <!-- 在编译和测试的过程有效,最后生成war包时不会加入 -->
        <scope>provided</scope>
    </dependency>

  </dependencies>

  <!-- 用来支持项目发布到私服中,用来配合deploy插件的使用 -->
  <distributionManagement>
      <!-- 发布版本 -->
    <repository>
        <id>releases</id>
        <name>public</name>
        <url>http://10.200.11.21:8081/nexus/content/repositories/releases/</url>
    </repository>
    <!-- 快照版本 -->
    <snapshotRepository>
        <id>snapshots</id>
        <name>Snapshots</name>
        <url>http://10.200.11.21:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
  </distributionManagement>

  <!-- 注意体会插件配置的顺序,这正体现了一个maven的运行流程 -->
  <build>
      <plugins>
          <!-- 插件使用练习 -->
          <!-- 清理插件的使用,maven3.0.4会默认使用2.4.1版本的clean插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>${plugin.version}</version>
            <executions>
                <execution>
                    <id>auto-clean</id>
                    <!-- clean生命周期clean阶段 -->
                    <phase>clean</phase>
                    <goals>
                        <!-- 执行clean插件的clean目标 -->
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- maven-resources-plugin在maven3.0.4中默认使用2.5版本的resources -->

        <!-- 编译插件的使用,maven3.0.4会默认使用2.3.2版本的compile插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${plugin.version}</version>
            <configuration>
                <!-- 源代码使用的jdk版本 -->
                <source>1.7</source>
                <!-- 构建后生成class文件jdk版本 -->
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- maven-surefire-plugin插件,maven3.0.4默认使用2.10版本的surefire插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${plugin.version}</version>
            <configuration>
                <!-- 改变测试报告生成目录 ,默认为target/surefire-reports-->
                <!-- project.build.directory表示maven的属性,这里指的是构建的目录下面test-reports,project.build.directory就是pom标签的值 -->
                <reportsDirectory>${project.build.directory}/test-reports</reportsDirectory>
            </configuration>
        </plugin>

        <!-- war包插件的使用,maven3.0.4会默认使用xxx版本的war插件,建议配置编码格式和打包名称 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <!-- 利用属性传递版本号 -->
            <version>${plugin.version}</version>
            <configuration>
                <!-- 设置编码 -->
                <encoding>UTF-8</encoding>
                <!-- 设置名称 -->
                <warName>ROOT</warName>
            </configuration>
        </plugin>

        <!-- maven-install-plugin插件一般不需要配置,maven3.0.4默认使用2.3.1版本的install插件 -->

        <!-- 部署插件的使用,maven3.0.4会默认使用2.7版本的deploy插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>${plugin.version}</version>
            <configuration>
                <!-- 更新元数据 -->
                <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
        </plugin>

    </plugins>
  </build>

</project>
  • profile
  • 在开发过程中,我们的项目会存在不同的运行环境,比如开发环境、测试环境、生产环境,而我们的项目在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。
  • maven则提供了一种更加灵活的解决方案,就是profile功能。
<profiles>
        <profile>
            <!--不同环境Profile的唯一id-->
            <id>dev</id>
            <properties>
                <!--profiles.active是自定义的字段(名字随便起),自定义字段可以有多个-->
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>
  • 可以看到定义了多个profile,每个profile都有唯一的id,也包含properties属性。这里为每个profile都定义一个名为profiles.active的properties,每个环境的值不同。当我们打包项目时,激活不同的环境,profiles.active字段就会被赋予不同的值。
  • 这个profiles.active字段可以应用到许多地方,及其灵活。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的程序猿~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值