1.目录结构:
1.新建文件夹C:\project\project-java\src;
2.在src建俩层main&test;
3.分别在main与test建俩层java和resources
2.新建java项目
- 在main的java中新建java项目Demon.java
package com.WU;
public class Demon{
public String say(String name){
System.out.println("hello "+name);
return "hello "+name;
}
}
-
在这个Java文件夹新建com文件夹和WU文件夹,并将java项目移入WU中,再把WU移入com中,形成
C:\project\project-java\src\main\java\com\WU----->Demon.java
3.测试文件
- 现在我们要写该项目的测试文件,在test的java文件夹中新建DemonTest.java
package com.WU;
import org.junit.Test;
import org.junit.Assert;
public class DemonTest{
@Test
public void testSay(){
Demon d =new Demon();
String ret = d.say("maven");
Assert.assertEquals("hello maven",ret);
}
}
-
也在这个Java文件夹新建com文件夹和WU文件夹,并将java项目移入WU中,再把WU移入com中,形成
C:\project\project-java\src\test\java\com\WU------>DemonTest.java
4.编写xml
-
查找并查看任意一个pom.xml,在src的同级目录新建一个pom.xml(C:\project\project-java),在其基础上临摹出以下
<?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. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.WU</groupId> <artifactId>project-java</artifactId> <version>1.0</version> <packaging>jar<packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
5.maven项目构建命令
-
命令
mvn compile #编译 mvn clean #清理 mvn test #测试 mvn package #打包 mvn install #安装到本地仓库
-
执行mvn compile:
-
在src同级目录(也与编写的pom.xml同级)执行cmd
cd C:\project\project-java
-
执行命令mvn compile,发现报错:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile..........
通过调查排除发现未指定jdk版本造成;
我们在编写的pom.xml中指定,加入以下命令:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
*我的jdk版本是1.8
再次执行问题解决!
eg我的pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.WU</groupId> <artifactId>project-java</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
结果:
[INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\project\project-java\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 35.816 s [INFO] Finished at: 2022-04-19T20:07:51+08:00 [INFO] ------------------------------------------------------------------------
-
-
执行mvn clean:
成功
-
再次执行mvn compile
-
执行mvn test
------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.WU.DemonTest hello maven Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.787 s [INFO] Finished at: 2022-04-19T20:35:41+08:00 [INFO] ------------------------------------------------------------------------
测试的详细信息在C:\project\project-java\target中的surefire-reports文件中,里面有一个测试名.xml,会有所有详细信息
-
执行mvn package
- 会自动先compile在test最后再package
-
执行mvn install
- 会在你定义的下载目录中eg:C:\maven\repository\com\WU