Maven配置、第三方依赖jar包打包以及Profiles多环境配置

由一次打包部署失败引发的深入探索┑( ̄▽  ̄)┍

一、Maven配置

1、概览

<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>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <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>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

2、基本配置

groupId+artifactId+version:组成项目的唯一定位,当groupId和version是从父级继承时,则不必显式定义他们;
packaging:定义打包方式,当前主要的打包方式有pom、 jar、 maven-plugin、 ejb、 war、 ear、 rar,其中jar为默认打包方式;
properties:定义pom常量,pom常量可以在pom文件的任意地方通过${}来引用;

3、构建配置

构建配置分为两种,"Project Build"和"Profile Build"

<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
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>

  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>

build:定义项目的目录结构和插件管理;

<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter1.properties</filter>
  </filters>
  ...
</build>

defaultGoal:指定默认的目标或者阶段,比如目标可以是jar:jar,阶段可以是install,这两个效果相同;
directory:构建的目标目录,默认是${basedir}/target;
finalName:项目最终生成的名字,默认是${artifactId}-${version};
filters:定义需要应用的*.properties文件。换句话说就是,在构建时筛选器文件中定义的“name=value”会替换掉资源文件中${name}字符串。Maven的默认筛选器目录是${basedir}/src/main/filters;
resources:资源列表,描述与项目关联的文件的内容和位置

  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    ...
  </build>

targetPath:定义项目生成后资源文件放置的位置,默认位置是${basedir};
filtering:可选值为true或false,定义是否为此资源启用筛选。与filters、profile元素结合使用;
directory:定义在哪找到资源文件,默认位置是${basedir}/src/main/resources;
includes:定义directory下需要包含的资源文件,支持*作为通配符匹配文件名;
excludes:结构同includes,定义directory下需要忽略的资源文件,当includes和excludes有冲突时,以excludes为准;

4、环境配置

profiles:根据不同的构建环境更改设置;

<profiles>
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>

activation:描述当前profile激活的条件,当activation里面描述的条件都满足时,就会激活当前profile;

<profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>sparrow-type</name>
          <value>African</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>

activeByDefault:是否是默认激活;
jdk:运行环境的jdk版本号;
os:运行环境的操作系统的属性;
file:哪些文件存在或者哪些文件缺失;

二、第三方依赖jar包打包

Maven项目有时候会遇到有第三方依赖打包的情况,如果这个时候不想deploy到Maven库上,可以这样做:
打jar包
1、新建一个目录用于存放第三方依赖,目录位置看个人喜好,这里新建lib放在项目根目录下;
2、pom文件中添加依赖;

<dependency>
        <groupId>xx</groupId>
        <artifactId>xx</artifactId>
        <version>2.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/xx-xx-2.0.jar</systemPath>
    </dependency>

3、在pom文件的build元素下添加resource配置,目的是将lib里的第三方依赖打到BOOT-INF/lib/下。这里还需要再配置另一个resource,因为添加前面这个配置后会覆盖掉默认的resource配置导致项目原本的资源文件没有打进构建生成的包里;

 <resources>
        <resource>
            <directory>lib</directory>
            <targetPath>BOOT-INF/lib/</targetPath>
            <includes>
                <include>**/*.jar</include>
            </includes>
         </resource>
         <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>

打war包
1、2步骤同jar包,3步骤需要这样修改

<!--设置maven-war-plugins插件 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <webResources>
                <resource>
                    <directory>lib</directory>
                    <targetPath>WEB-INF/lib/</targetPath>
                    <includes>
                        <include>**/*.jar</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>

三、Profiles多环境配置

1、新建一个目录用于存放filter配置文件,这里新建filters目录,并存在localhost.properties、test.properties、prod.properties三个环境的配置文件。
2、pom文件中添加profiles配置

<profiles>
        <profile>
            <id>localhost</id>
            <properties>
                <profiles.active>localhost</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

3、pom文件中的build元素下添加配置

<resources>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>
<filters>
    <filter>${basedir}/filters/${profiles.active}.properties</filter>
</filters>

4、${basedir}/src/main/resources目录下编写总配置文件,使用${name}占位符(SpringBoot项目使用@[email protected]占位符)来获取filters中配置文件对应环境的配置值

参考文章:
https://www.jianshu.com/p/574f74d1d0ee
http://maven.apache.org/pom.html

原文地址:https://www.cnblogs.com/irain/p/12030646.html

时间: 2024-08-03 13:39:22

Maven配置、第三方依赖jar包打包以及Profiles多环境配置的相关文章

springboot解决第三方依赖jar包的问题

公司现在用的是springboot+maven,想要把一些老的项目都改成这种框架.但是一些老的项目中有好多第三方的jar包或者是自己的jar包,maven库上没有.最初的解决方案是一个个的deploy到maven库上,但是遇到太多的三方jar包就太费事了.网上查了一下发现maven有一种方式可以将本地的Jar包依赖到项目中而不需要先deploy到maven库上.具体做法如下: 跟普通的java项目一样,新建个lib目录放jar包,我建在了src下. pom文件中依赖这样写: <!--添加外部依赖

spring maven项目解决依赖jar包版本冲突方案

引入:http://blog.csdn.net/sanzhongguren/article/details/71191290 在spring reference中提到一个解决spring jar包之间版本冲突的解决方案,原文如下 It is possible to accidentally mix different versions of Spring JARs when using Maven. For example, you may find that a third-party lib

maven笔记-将本地jar包打包进可执行jar中

参考资料:http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html 使用本地jar <dependencies> <dependency> <groupId>org.richard</groupId> <artifactId>my-jar</artifactId> <version>1.0</version> <s

maven环境的配置,如果jar包下载不下来,其他配置无错误的话,极有可能的网速的缘故

1首先下载apach maven 2配置maven环境变量 m2_home  maven的源文件的路径 path变量后跟 %m2_home%\bin 3cmd 控制台运行mvn -version 查看版本出来即可 ps : 如果从maven仓库下完copy进文件夹中时,记得updatesetting即可

[Maven]Maven构建可执行的jar包(包含依赖jar包)

----------------------------------------------------------------- 转载请注明出处! 博主:疲惫的豆豆 链接:http://www.cnblogs.com/dzblog/p/6913809.html ----------------------------------------------------------------- 目标: 将依赖的第三方jar包打进去 方法: maven-assembly-plugin 环境: IDE

maven依赖本地非repository中的jar包-依赖jar包放在WEB-INF/lib等目录下的情况客户端编译出错的处理

maven依赖本地非repository中的jar包 http://www.cnblogs.com/piaolingxue/archive/2011/10/12/2208871.html 博客分类: MAVEN 今天在使用maven编译打包一个web应用的时候,碰到一个问题: 项目在开发是引入了依赖jar包,放在了WEB-INF/lib目录下,并通过buildpath中将web libariary导入. 在eclipse中开发没有问题,但是使用maven编译插件开始便宜总是报找不到WEB-INF

Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图

Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有: 第一个按钮:Reimport All Maven Projects 表示根据 pom.xml 重新载入项目.一般单我们在 pom.xml 添加了依赖包或是插件的时候,发现标注 4 的依赖区中没有看到最新写的依赖的话,可以尝试点击此按钮进行项目的重新载入. 第六个按钮:Execute Maven Goal 弹出可执行的 Maven 命令的输入框.有些情况下我们需要通过书写某些执行命令来构建项目,就可以通

Eclipse下新建Maven项目、自动打依赖jar包

当我们无法从本地仓库找到需要的构件的时候,就会从远程仓库下载构件至本地仓库.一般地,对于每个人来说,书房只有一个,但外面的书店有很多,类似第,对于Maven来说,每个用户只有一个本地仓库,但可以配置访问很多远程仓库. Eclipse *版本 Eclipse *下载 强烈推荐书籍:Maven实战   许晓斌著.目前是第一版 注意:最新版本的Eclipse代号Mars,已经直接集成了Maven,所以无需安装m2Eclipse插件. Eclipse下新建Maven项目 1.下载Maven安装包 进入M

maven依赖jar包更新,业务jar需同步更新(业务jar依赖API)

背景: 环境出现问题,定位为依赖jar缺失,修改工程pom文件补充依赖jar. 更新要点说明: 依赖jar,更新提交 业务jar,也需更新提交:maven构建会把依赖jar引用进去,更新环境如果单独更新了依赖jar包,新构建业务jar内部api也不一致,业务jar也得更新 例: 我依赖的jar包里的一个方法,本来返回值是void,更新jar后,现在返回boolean 对我代码没有影响,我代码可以不改,但是,如果不重新编译打个我这边的jar包,就用之前我开发的jar的话,jar里的class还是会