首页 > 其他分享 >?Maven踩坑实录

?Maven踩坑实录

时间:2023-10-23 12:06:41浏览次数:28  
标签:xml maven project 仓库 标签 实录 Maven mvn

本文记录学习or工作时一些Maven常见操作以及踩坑的地方,以此文做以记录。

一、Maven如何使用私服地址

mvn install的时候想要的是从私服下载jar包,此处我就踩了坑。

SpringBoot项目,通过mvn install,Maven一直从maven2仓库进行下载,我明明没有在项目或者setting.xml中配置Maven2仓库的url路径啊?

查阅资料得知,原来 springboot的pom文件都继承了super pom.而super pom中配置的就是maven2,关于如何查询maven2配置,路径如下:

$MAVEN_HOME/lib/maven-model-builder-xxx/org/apache/maven/model/pom-4.0.0.xml


<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

pom-4.0.0.xml

可以看到是在此文件中配置了maven2仓库地址: https://repo.maven.apache.org/maven2

项目报错信息如下:

?Maven踩坑实录_maven

 下面来了解下Maven中repositories、pluginRepositories、distributionManagement标签的含义

在pom.xml文件中,repositories、pluginRepositories和distributionManagement是三个常用的标签,它们分别用于定义项目的仓库配置和分发部署配置。

  1. repositories标签:
    repositories标签用于定义项目的仓库配置,即项目从哪些远程仓库获取依赖项。在repositories标签中,可以定义多个repository子标签,每个repository标签包含以下属性:
  • id: 仓库的唯一标识符。
  • url: 仓库的URL地址。
  • name: 仓库的名称。
  • releases: 是否使用该仓库获取发布版本的依赖项。
  • snapshots: 是否使用该仓库获取快照版本的依赖项。
  1. pluginRepositories标签:
    pluginRepositories标签用于定义项目的插件仓库配置,即从哪些远程仓库获取插件依赖项。它的用法和repositories标签类似,可以定义多个pluginRepository子标签。
  2. distributionManagement标签:
    distributionManagement标签用于定义项目的分发部署配置,即项目如何分发部署到远程仓库或者发布到指定的服务器。在distributionManagement标签中,可以定义以下子标签:
  • repository: 用于指定发布到远程仓库的配置,包括URL和唯一标识符等。
  • snapshotRepository: 用于指定发布快照版本到远程仓库的配置。
  • site: 用于指定发布项目站点的配置,包括URL和唯一标识符等。
  • downloadUrl: 用于指定分发下载地址的URL。

这些标签的配置可以使得项目的依赖管理更加方便和灵活,并且可以通过分发部署配置实现项目的发布、安装和部署等操作。

如何解决不从私服下载jar包的问题

项目中配置解决:

<repositories>
        <repository>
            <id>releases.asp.com</id>
            <url>http://202.85.222.14:7038/repository/maven-public/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>releases.asp.com</id>
            <url>http://202.85.222.14:7038/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>

pom.xml

?Maven踩坑实录_maven_02

然后重新的编译项目:  mvn clean install -Dmaven.test.skip=true

二、Maven指定jdk编译版本

之前有个项目

项目开发版本:jdk1.8

部署的服务器上jsk版本:1.7

------------------------------------------------------------

先说我踩坑的地方,我知道是这个问题,在服务器上面通过mvn install是不通过的,报错信息如下:

?Maven踩坑实录_maven_03

下面说两种解决办法:

第一种:通过shell脚本

在shell脚本中指定jdk版本。于是像这样:export JAVA_HOME=/usr/local/java/jdk1.8.0_144

#!/bin/bash
export JAVA_HOME=/usr/local/java/jdk1.8.0_144

cd /root/maven_repo_demo/healthproduct_parent
mvn clean install

第二种:设置mvn命令环境变量

为了不修改之前的mvn全局配置,这里重新复制出来一份mvn,起名为mvn8,表示是jdk8环境: cp mvn mvn8 && vim mvn8

export JAVA_HOME=/usr/local/java/jdk1.8.0_144

?Maven踩坑实录_maven_04

 再使用命令编译项目即可,如下所示:

/usr/local/maven/apache-maven-3.6.3/bin/mvn8 clean install

三、Maven使用指定的配置(setting.xml)

在一个服务器上面,可能有多个项目,免不了有的需求,指定这个项目使用xxx仓库,这时候就需要在编译的时候单独指定setting.xml位置了(setting中指定仓库新地址)

1.设置maven的配置路径,同时需要在setting.xml中指定maven仓库的位置

mvn clean install -s [setting.xml路径]
mvn clean instal -s D:\StudySoftware\Maven\apache-maven-3.6.3\conf\settings_diy.xml

2.单独设置maven的仓库地址,此方法用的还是默认setting.xml,只是重新指定了仓库地址

mvn clean install -Dmaven.repo.local=[需要指定的maven仓库地址]
mvn clean install -Dmaven.repo.local=D:\StudySoftware\Maven\apache-maven-3.6.3\maven_diy

  

 



标签:xml,maven,project,仓库,标签,实录,Maven,mvn
From: https://blog.51cto.com/zhangzhixi/7985378

相关文章

  • Maven基础
    官网:https://maven.apache.org/目录结构安装一一般安装后通过idea中的插件使用maven坐标依赖范围生命周期......
  • Maven中的dependencyManagement 详解
    1.作用:在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器。2.pom.xml文件中,jar的版本判断的两种途径:(1)如果dependencies里的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupI......
  • springboot使用maven打成jar包,jar包无法找到主清单类
    <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.7.7......
  • linux yum安装maven
    安装mavenwgethttp://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo-O/etc/yum.repos.d/epel-apache-maven.repoyum-yinstallapache-maven查看mavenhomemvn-v 配置环境变量vi/etc/profile重载环境变量source/etc/profile......
  • 将达梦数据库的JDBC驱动包 DmJdbcDriver18.jar 安装到本地 Maven 仓库
    将达梦数据库的JDBC驱动包DmJdbcDriver18.jar安装到本地Maven仓库 一、问题背景在博客《SpringBootMyBatisPlus整合达梦数据库》中写道,从 https://eco.dameng.com/download/ 中下载达梦JDBC驱动包,如下: JDK1.8 对应的JDBC驱动包为:DmJdbcDriver18.jar ......
  • Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:res
    Failedtoexecutegoalorg.apache.maven.plugins:maven-resources-plugin:3.2.0:resources 一、问题背景在SpringBoot工程编译过程中,出现报错信息:“Failedtoexecutegoalorg.apache.maven.plugins:maven-resources-plugin:3.2.0:resources”。 效果如下: 二、解......
  • Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found
    Plugin'org.springframework.boot:spring-boot-maven-plugin:'notfound 一、问题现象pom.xml文件中有报红的错误提示,“Plugin'org.springframework.boot:spring-boot-maven-plugin:'notfound”。pom.xml中的信息如下:<build><plugins>......
  • Maven
    0.MavenMaven是apache旗下的一个开源项目,用于管理和构建Java项目依赖管理,jar包,避免版本冲突问题统一项目结构,src下的main存放实际项目资源、test测试项目资源,java源代码,resources配置文件目录,pom.xml项目配置文件项目构建标准跨平台的自动化项目构建方式1.介绍基于项目......
  • JavaWeb-Maven的应用
    目录1.MavenPOM2.pom.xml内容MavenPOMPOM(ProjectObjectModel,项目对象模型)是Maven工程的基本工作单元,是一个XML文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。执行任务或目标时,Maven会在当前目录中查找POM。它读取POM,获取所需的配置信息,然......
  • IdeaMaven换源(保姆级别演示)
    (1)在Idea中找到settings这个按钮,然后点击进去,如下图所示。(2)然后在里面找到Buuild,Execution,Deployment,按钮(3)点击里面的BuildTools找到Maven,将Usersettingsfile这个框后面的Override勾选上(4)点击OK(5)再在这个Usersettingsfile里面的路径.m下现新建先新建一个文本文档,命名为setti......