Maven安装
Linux安装
1、下载maven的yum源 #>wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo 2、安装maven #>yum install -y maven 3、查看maven版本信息 #>mvn -version
直接yum安装,base镜像里面的版本
使用Maven的源,然后安装,看看版本是不是更新,看了下没啥变化,没查出新的仓库里面有Maven,这里就先忽略了
[root@mcw12 demo1]# mvn --version Apache Maven 3.0.5 (Red Hat 3.0.5-17) Maven home: /usr/share/maven Java version: 1.8.0_412, vendor: Red Hat, Inc. Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix" [root@mcw12 demo1]#
升级maven
https://apache.osuosl.org/maven/maven-3/3.8.8/binaries/
# 更新Java(以Oracle JDK 1.8为例) sudo apt-get install openjdk-8-jdk # 以Ubuntu为例 # 设置JAVA_HOME环境变量 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 # 更新Maven wget https://apache.osuosl.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz tar -xvf apache-maven-3.6.3-bin.tar.gz # 设置M2_HOME环境变量 export M2_HOME=/path/to/apache-maven-3.6.3 # 更新PATH环境变量 export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
[root@mcw12 opt]# mvn --version Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39) Maven home: /opt/apache-maven-3.8.8 Java version: 1.8.0_412, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix" [root@mcw12 opt]#
安装java17
首先,我们需要确认当前系统是否已经安装了 Java。可以通过运行以下命令来检查:
java -version
如果系统已经安装了 Java,将显示 Java 的版本信息。如果没有安装,我们需要先安装 Java。
要安装 Java 17,我们需要从 Oracle 的官方网站下载适合 CentOS 7/RHEL 7 的 JDK 17 二进制文件。可以使用 wget 命令下载,例如:
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
下载完成后,我们需要使用 rpm 命令安装下载的 JDK 17 二进制文件:
rpm -ivh jdk-17_linux-x64_bin.rpm
安装完成后,我们可以再次运行 java -version
命令来检查是否成功安装了 Java 17。
注意:在安装过程中可能会遇到一些依赖问题,需要手动解决这些依赖问题。例如,如果系统缺少 libaio 或 libaio1,可以使用以下命令进行安装:
sudo yum install libaio libaio-devel
另外,还需要注意系统架构是否与 JDK 17 二进制文件匹配。例如,如果系统是 x86 架构,需要下载 x86 架构的 JDK 17 二进制文件。
安装完成后,可以通过运行以下命令来验证 Java 是否正确安装:
jaclin -version
java -version
以上就是在 CentOS 7/RHEL 7 上安装 Java 17 (OpenJDK 17) 的基本步骤。需要注意的是,在生产环境中,应该根据实际需求选择合适的 Java 版本,并进行合理的配置和管理。
操作之前 [root@mcw12 ~]# java -version openjdk version "1.8.0_412" OpenJDK Runtime Environment (build 1.8.0_412-b08) OpenJDK 64-Bit Server VM (build 25.412-b08, mixed mode) [root@mcw12 ~]#
操作之后 [root@mcw12 ~]# jaclin -version -bash: jaclin: command not found [root@mcw12 ~]# java -version java version "17.0.11" 2024-04-16 LTS Java(TM) SE Runtime Environment (build 17.0.11+7-LTS-207) Java HotSpot(TM) 64-Bit Server VM (build 17.0.11+7-LTS-207, mixed mode, sharing) [root@mcw12 ~]#
常用maven使用
mvn clean 会把原来target目录给删掉重新生成。
mvn install 安装当前工程的输出文件到本地仓库,然后打包
mvn clean install 先删除target文件夹 ,然后打包到target
1、Maven打包跳过测试的命令
在使用mvn package进行编译、打包时,Maven会执行src/test/java中的JUnit测试用例,有时为了跳过测试,会使用参数-DskipTests和-Dmaven.test.skip=true,这两个参数的主要区别是:
-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
-Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。
类似的 跳过doc生成
-Dmaven.javadoc.skip=true
2、打包时注意:
2.1)当mvn仓库里缺少jar包,同时又从中央仓库自动下载不下来的时候,就需要自己下载jar包然后放仓库里了,
但是有时候只是简单的把jar和source放仓库的文件夹下,并不管用,这个时候你可以用命令把jar把打进去:
mvn install:install-file -Dfile=D:\xxx.jar -DgroupId=commons-dbcp -DartifactId= commons-dbcp -Dversion= 1.4 -Dpackaging=jar
-Dfile 是存在本地磁盘里jar 的路径,后面的就不用说了吧!install:install-file 看清楚了!!!这个-file跟install是连着的。
2.2)mvn dependency:tree命令解决jar包冲突
当项目出现jar包冲突时,用命令mvn dependency:tree 查看依赖情况
mvn dependency:tree 查看依赖树,查看包结构间的依赖
mvn dependency:tree >d:/tmp 把结果输出到文件,
然后再pom.xml文件里排除掉冲突的jar包
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${tiles.version}</version>
<exclusions>
<exclusion>
<groupId> org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
解决依赖关系,根据POM文件,下载或者更新该项目所依赖的库文件:
mvn dependency:resolve-plugins
centos上Maven的使用演示
准备代码
我这里直接将代码整体复制到Linux服务器上。一般情况是git从gitlab上拉取代码
将代码已经上传上去了
执行命令失败maven版本不同
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:3.3.2:clean (default-clean) on project demo1: The plugin org.apache.maven.plugins:maven-clean-plugin:3.3.2 requires Maven version 3.2.5 -> [Help 1]
执行命令时自动下载的版本是3.3.2
而我的maven版本是3.0.5。我升级下自己的maven,Maven version应该要大于 3.2.5
升级之后查看。升级方法见上面章节
[root@mcw12 opt]# mvn --version Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39) Maven home: /opt/apache-maven-3.8.8 Java version: 1.8.0_412, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix" [root@mcw12 opt]#
再次执行
可以看到已经成功删除target目录
[root@mcw12 demo1]# ls HELP.md mvnw mvnw.cmd pom.xml src target [root@mcw12 demo1]# mvn clean [INFO] Scanning for projects... [INFO] [INFO] -------------------------< org.example:demo1 >-------------------------- [INFO] Building demo1 0.0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.2:clean (default-clean) @ demo1 --- Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/4.0.0/plexus-utils-4.0.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/4.0.0/plexus-utils-4.0.0.pom (8.7 kB at 7.1 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/13/plexus-13.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/13/plexus-13.pom (27 kB at 128 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.9.3/junit-bom-5.9.3.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.9.3/junit-bom-5.9.3.pom (5.6 kB at 37 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/4.0.0/plexus-utils-4.0.0.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/4.0.0/plexus-utils-4.0.0.jar (192 kB at 531 kB/s) [INFO] Deleting /root/demo1/target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.107 s [INFO] Finished at: 2024-07-07T21:39:04+08:00 [INFO] ------------------------------------------------------------------------ [root@mcw12 demo1]# ls HELP.md mvnw mvnw.cmd pom.xml src [root@mcw12 demo1]#
重新打包,报错
[root@mcw12 demo1]# ls HELP.md mvnw mvnw.cmd pom.xml src [root@mcw12 demo1]# mvn pacage [INFO] Scanning for projects... [INFO] [INFO] -------------------------< org.example:demo1 >-------------------------- [INFO] Building demo1 0.0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.568 s [INFO] Finished at: 2024-07-07T21:40:34+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Unknown lifecycle phase "pacage". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException [root@mcw12 demo1]#
将项目下的这段注释掉,用其它的替代试试
用下面的替代
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.yourcompany.yourapp.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 添加maven-assembly-plugin来创建带有所有依赖的fat JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.yourcompany.yourapp.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
重新打包之后,报错了
Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0.3/qdox-2.0.3.jar (334 kB at 352 kB/s) [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 3 source files with javac [debug parameters release 17] to target/classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /root/demo1/src/main/java/com/example/demo/Demo1Application.java:[3,32] cannot access org.springframework.boot.SpringApplication bad class file: /root/.m2/repository/org/springframework/boot/spring-boot/3.3.1/spring-boot-3.3.1.jar(org/springframework/boot/SpringApplication.class) class file has wrong version 61.0, should be 52.0 Please remove or make sure it appears in the correct subdirectory of the classpath. [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 43.995 s [INFO] Finished at: 2024-07-07T21:54:24+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project demo1: Compilation failure [ERROR] /root/demo1/src/main/java/com/example/demo/Demo1Application.java:[3,32] cannot access org.springframework.boot.SpringApplication [ERROR] bad class file: /root/.m2/repository/org/springframework/boot/spring-boot/3.3.1/spring-boot-3.3.1.jar(org/springframework/boot/SpringApplication.class) [ERROR] class file has wrong version 61.0, should be 52.0 [ERROR] Please remove or make sure it appears in the correct subdirectory of the classpath. [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException [root@mcw12 demo1]# ls HELP.md mvnw mvnw.cmd pom.xml src target [root@mcw12 demo1]# ls target/ classes generated-sources maven-status [root@mcw12 demo1]#
好像是版本高了
我们Linux服务器是jdk8,而这个项目使用java 17 进行构建和运行的。
标签:INFO,maven,version,使用,apache,org,root From: https://www.cnblogs.com/machangwei-8/p/18288999