🍕Maven踩坑实录

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

Maven踩坑实录

一、Maven如何使用私服地址

  在工作中,有的jar包是放在私服上面的,这时候我们在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中repositories、pluginRepositories、distributionManagement标签的含义

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

  1. repositories标签:
    repositories标签用于定义项目的仓库配置,即项目从哪些远程仓库获取依赖项。在repositories标签中,可以定义多个repository子标签,每个repository标签包含以下属性:

    • id: 仓库的唯一标识符。
    • url: 仓库的URL地址。
    • name: 仓库的名称。
    • releases: 是否使用该仓库获取发布版本的依赖项。
    • snapshots: 是否使用该仓库获取快照版本的依赖项。
  2. pluginRepositories标签:
    pluginRepositories标签用于定义项目的插件仓库配置,即从哪些远程仓库获取插件依赖项。它的用法和repositories标签类似,可以定义多个pluginRepository子标签。

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

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

二、Maven指定jdk编译版本

之前有个项目

项目开发版本:jdk1.8

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

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

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

下面说两种解决办法:

第一种:通过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

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

/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
如果有多个参数的话可以使用空格进行分割,如下:
mvn clean install -Dmaven.test.skip=true -Dmaven.repo.local=D:\StudySoftware\Maven\apache-maven-3.6.3\maven_diy

 

  

 

  

 

posted @ 2023-08-29 21:30  Java小白的搬砖路  阅读(42)  评论(0编辑  收藏  举报