Maven 常用配置备注

JDK编译版本的插件

 <!--jdk编译插件-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
    </configuration>
</plugin>

Tomcat7服务端的插件(部署项目)

<plugins>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
            <!-- 指定端口 -->
            <port>82</port>
            <!-- 请求路径 -->
            <path>/</path>
        </configuration>
    </plugin>
</plugins>

修改不使用骨架创建maven项目的默认编译版本

<profile>   
        <id>jdk1.8</id>
        <activation>   
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>   
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            <encoding>UTF-8</encoding>
        </properties>   
</profile>

将上述标签内容添加到settings文件的标签中

彻底解决引入依赖的时候卡、报错

修改settings.xml文件,添加如下代码

<mirrors>
      <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>
      </mirror>
      <mirror>
          <id>uk</id>
          <mirrorOf>central</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>http://uk.maven.org/maven2/</url>
      </mirror>

      <mirror>
          <id>CN</id>
          <name>OSChina Central</name>
          <url>http://maven.oschina.net/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>
      </mirror>

      <mirror>
          <id>nexus</id>
          <name>internal nexus repository</name>
          <url>http://repo.maven.apache.org/maven2</url>
          <mirrorOf>central</mirrorOf>
      </mirror>
  </mirrors>

把第三方 jar 包放入本地仓库和私服

mvn install:install-file -DgroupId=com.itheima -DartifactId=heimaMvc
-Dversion=1.0 -Dfile=heimaMvc-1.0.jar -Dpackaging=jar

导入私服

需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息

<server>
    <id>thirdparty</id>
    <username>admin</username>
    <password>admin123</password>
</server>

才能执行一下命令

mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=oracle -Dversion=2.0.3 -Dpackaging=jar -Dfile=oracle.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

参数说明

DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。自己起名字也行。

Dfile 表示需要上传的 jar 包的绝对路径。

Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。

DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。

Dversion 表示版本信息。

关于 jar 包准确的版本:

​ 包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件

​ 这个文件就有描述该包的版本信息。

​ 比如 Specification-Version: 2.2 可以知道该包的版本了。

​ 上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。

直接排除法

当我们需要排除某个jar包的依赖时,在配置exclusions标签的时候,内部可以不写版本号。pom.xml依赖如下:

<!--导入相关依赖包-->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.4.RELEASE</version>
        <!--直接排除-->
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>

在这里插入图片描述
真实项目中,出现1个项目存在多个同种jar包的时候,需要我们进行解决maven的jar包冲突问题(异常:Class not found, class not defined、Method not found, NoSuchField等)

无法通过依赖管理原则排除的,使用直接排除法

<exclusions>
    <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </exclusion>
</exclusions>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值