Maven知识汇总

    xiaoxiao2021-03-25  129

    修改远程仓库地址

    中央仓库(Maven Central Repository)

    Maven有一个默认的远程仓库,id为central,是Apache搭建的,默认情况下都会去这个仓库查找获取文件,因为服务器在国外,所以速度很感人,因此通常我们会使用国内的库来代替中央仓库。

    镜像(mirror)

    Maven提供了一个映射机制来实现远程仓库的切换,使用时要配置该镜像的被映射目标对象,可以是一个或多个,*为全部映射,当请求被映射的仓库,会从之前的仓库切到该镜像中的仓库地址进行查找获取文件,从而达到切换远程仓库的目的。

    可以同时配置多个mirror,但镜像匹配顺序是按照mirror的配置顺序依次匹配的,前面的mirror匹配成功,则后面的mirror不会生效,因此,不能用mirror来实现配置多个仓库,mirror的作用主要是用于仓库加速。

    配置格式:在maven的settings.xml文件中进行如下配置:

    <mirrors> <mirror> <!--该镜像的id--> <id>nexus-aliyun</id> <!--被该镜像映射的远程仓库,central是中央仓库的id,即我们将中央仓库映射到了阿里云的仓库--> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <!--该镜像的仓库地址,这里是用的阿里的仓库--> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>

    对于各配置项,官方解释:

    id, name: The unique identifier and user-friendly name of this mirror. The id is used to differentiate between mirror elements and to pick the corresponding credentials from the `` section when connecting to the mirror.url: The base URL of this mirror. The build system will use this URL to connect to a repository rather than the original repository URL.mirrorOf: The id of the repository that this is a mirror of. For example, to point to a mirror of the Maven central repository (https://repo.maven.apache.org/maven2/), set this element to central. More advanced mappings like repo1,repo2 or *,!inhouse are also possible. This must not match the mirror id.

    说明:

    id是镜像的唯一标识符;mirrorOf配置的是被映射仓库的规则,例如需要将中央仓库central映射到其它第三方库,则mirrorOf配置central;要将所有仓库都进行映射则mirrorOf配置*;如果要将repo1和repo2两个仓库都进行映射,则mirrorOf配置repo1,repo2;如果要将除inhouse仓库外其它所有仓库进行映射,则mirrorOf配置*,!inhouse;需要注意的是,mirrorOf中配置的id不能和mirror id相同,否则会出问题,所以在设置仓库的id和镜像的id时注意区分;

    配置多个仓库

    通常公司内部会搭建自己的Maven仓库,里面会发布一些内部软件包,所以我们可能需要配置多个仓库。

    多个仓库的配置如下:

    <profiles> <profile> <id>snapshots</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <!--仓库1--> <repository> <id>nexus-aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </repository> <!--仓库2--> <repository> <!--仓库唯一id--> <id>nexus-myself</id> <url>http://localhost:8080/nexus/content/groups/public</url> <snapshots> <!--是否启用--> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles>

    说明:

    updatePolicy说明:

    <!-- 每次执行构建命令时, Maven 会比较本地 POM 和远程 POM 的时间戳, 该元素指定比较的频率。 | 有效选项是: | always(每次构建都检查), daily(默认, 距上次构建检查时间超过一天), interval: x(距上次构建检查超过 x 分钟)、 never(从不) | | 重要: | 设置为 daily, 如果 artifact 一天更新了几次, 在一天之内进行构建, 也不会从仓库中重新获取最新版本 -->

    配置多个仓库时,使用的优先级为配置顺序从上到下,第一个查找不到,则从接下来的仓库中查找,直至中央仓库,上面配置的仓库使用顺序为nexus-myself -> nexus-aliyun -> central

    设置工程源目录

    Maven通常将src/main/java和src/main/resources作为默认的源目录,并且只会编译src/main/java中的java文件,如果有xml等文件就会被忽略,然而有时我们又需要在src/main/java中放置一些其他类型文件,例如放置mybatis的mapper映射文件。

    我们可以通过设置resources标签来控制编译的源目录

    <build> <resources> <resource> <directory>src/main/java</directory> <!--设置需要包含的其它文件类型的路径及后缀--> <!--**表示所有层级目录,*Mapper.xml表示文件名以Mapper结尾的所有xml文件--> <includes> <include>**/*Mapper.xml</include> </includes> </resource> <!--手动设置resources后,resources目录不会被默认编译,也需要手动设置--> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>

    基于Maven的web工程目录结构

    src --main ----java ------com.xxx.xxx ------WebContent --------META-INF ----------MANIFEST.MF --------WEB-INF ----------lib ----------web.xml ----resources --test ----java ------com.xxx.xxx target pom.xml

    其中src/main/java、src/main/resources、src/test/java为工程源目录。

    使用中的问题记录

    无效的目标发行版: 1.8

    遇到此问题需要确认各个部位使用的Java版本是否一致:

    IDE的使用的JDK版本;环境变量中的Java版本;如果是Eclipse,那么run configurations => maven build,选中下面的命令,里面的JRE也需要设置为一致版本的Java;

    Unsupported major.minor version 51.0

    org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0


    -Dmaven.multiModuleProjectDirectory system property is not set.

    这种情况是因为虚拟机运行参数没有设置maven的环境变量,解决方案如下:

    一、在环境变量的系统变量中设置M2_HOME变量,值就是Maven的安装路径

    二、如果是Eclipse可如下设置:

    install jre -> edit -> default vm arguments添加**-Dmaven.multiModuleProjectDirectory=$M2_HOME**


    程序包com.sun.org.apache.xml.internal.security.utils不存在

    问题原因:代码中使用了sun公司的第三方私有库,导致编译不通过

    在pom文件中加入如下代码:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.build.sourceEncoding}</encoding> <!--配置编译参数--> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin>

    注意:中路径的分隔符,在Windows下是“;”,在Linux下是“:”。

    原文链接:https://www.cnblogs.com/xiluhua/p/5621279.html


    Maven工程控制台输出中文乱码

    在pom.xml的properties标签下加入如下元素即可解决

    <argLine>-Dfile.encoding=UTF-8</argLine>

    使用命令行创建简单Java项目

    mvn archetype:generate -DinteractiveMode=false -DarchetypeCatalog=internal -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.dongrui.study_poi -DartifactId=study-poi -Dpackage=com.dongrui.study_poi -Dversion=1.0
    转载请注明原文地址: https://ju.6miu.com/read-11241.html

    最新回复(0)