【Maven 官方教程】Building Java Projects

简介: Create the directory structuremkdir -p src/main/java/hello on *nix systems└── src └── main └── java └── hellocreate these two classes: HelloWorld.java and Greeter.java.src/main/java/hello/HelloWorld.java

Create the directory structure


mkdir -p src/main/java/hello on *nix systems

└── src
    └── main
        └── java
            └── hello


create these two classes: HelloWorld.java and Greeter.java.


src/main/java/hello/HelloWorld.java

package hello;
public class HelloWorld {
  public static void main(String[] args) {
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}


src/main/java/hello/Greeter.java

package hello;
public class Greeter {
  public String sayHello() {
    return "Hello world!";
  }
}


Define a simple Maven build


准备好Maven环境后, 你需要创建一个 Maven 项目的定义。 Maven 项目是用一个名为 pom.XML 的 XML 文件定义的。 除此之外, 这个文件提供了项目的名称、版本和它对外部库的依赖。


Create a file named pom.xml at the root of the project (i.e. put it next to the src folder) and give it the following contents:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


Build Java code


Maven is now ready to build the project. You can execute several build lifecycle goals with Maven now, including goals to compile the project’s code, create a library package (such as a JAR file), and install the library in the local Maven dependency repository.


mvn compile


This will run Maven, telling it to execute the compile goal. When it’s finished, you should find the compiled .class files in the target/classes directory.


mvn package


The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory. The name of the JAR file will be based on the project’s <artifactId> and <version>. For example, given the minimal pom.xml file from before, the JAR file will be named gs-maven-0.1.0.jar.


To execute the JAR file run:


java -jar target/gs-maven-0.1.0.jar


Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies. If you’d like to install your project’s JAR file to that local repository, then you should invoke the install goal:


mvn install


The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.


Declare Dependencies 声明依赖项



简单的 Hello World 样例是完全自给自足的, 不依赖于任何其他的库。 然而, 大多数应用程序依赖于外部库来处理公共和复杂的功能。


例如, 假设除了说"你好, 世界!" , 您希望应用程序打印当前的日期和时间。 虽然你可以使用本地 Java 库中的日期和时间设施, 但是使用 Joda Time 库可以使事情变得更有趣。

需要在pom文件中添加依赖


<dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.2</version>
        </dependency>
</dependencies>


默认情况下,所有依赖项的范围都是编译依赖项。 也就是说,它们应该在编译时可用(如果您正在构建 WAR 文件,包括 WAR 的 / web-inf / libs 文件夹)。 此外,您可以指定一个 scope 元素来指定以下范围之一:


  • provided-编译项目代码所需的依赖项, 但这些依赖将由运行代码的容器(例如 Java Servlet API)在运行时提供。


  • test-用于编译和运行测试的依赖项, 但不需要用于构建或运行项目的运行时代码。


Write a Test



First add JUnit as a dependency to your pom.xml, in the test scope:


<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>


Then create a test case like this:


src/test/java/hello/GreeterTest.java


package hello;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import org.junit.Test;
public class GreeterTest {
  private Greeter greeter = new Greeter();
  @Test
  public void greeterSaysHello() {
    assertThat(greeter.sayHello(), containsString("Hello"));
  }
}


Maven 使用一个叫做 "surefire" 的插件来运行单元测试。The default configuration of this plugin compiles and runs all classes in src/test/java with a name matching *Test. 你可以像这样在命令行上运行测试


mvn test


The completed pom.xml file is using the Maven Shade Plugin for the simple convenience of making the JAR file executable. The focus of this guide is getting started with Maven, not using this particular plugin. |




目录
相关文章
|
4天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
【5月更文挑战第3天】本文介绍了如何在Web自动化测试中使用JavaScript执行器(JavascriptExecutor)来完成Selenium API无法处理的任务。首先,需要将WebDriver转换为JavascriptExecutor对象,然后通过executeScript方法执行JavaScript代码。示例用法包括设置JS代码字符串并调用executeScript。文章提供了两个实战场景:一是当时间插件限制输入时,用JS去除元素的readonly属性;二是处理需滚动才能显示的元素,利用JS滚动页面。还给出了一个滚动到底部的代码示例,并提供了详细步骤和解释。
32 10
|
1天前
|
存储 JavaScript Java
《手把手教你》系列技巧篇(四十七)-java+ selenium自动化测试-判断元素是否显示(详解教程)
【5月更文挑战第11天】WebDriver 的 `isDisplayed()` 方法用于检查页面元素是否可见,如果元素存在于DOM中且可视,返回`true`,否则返回`false`。在自动化测试中,这个方法常用于验证元素是否真正显示在页面上。示例代码展示了如何使用 `isDisplayed()` 判断百度登录页面的特定错误提示文字是否出现。
11 1
|
2天前
|
JavaScript Java 测试技术
《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)
【5月更文挑战第10天】本文介绍了使用Java和Selenium进行Web自动化测试的实践,以安居客网站为例。最后,提到了在浏览器开发者工具中调试和观察页面元素的方法。
12 2
|
3天前
|
算法 Java Python
保姆级Java入门练习教程,附代码讲解,小白零基础入门必备
保姆级Java入门练习教程,附代码讲解,小白零基础入门必备
|
3天前
|
Web App开发 JavaScript 测试技术
《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)
【5月更文挑战第9天】本文介绍了在Appium中处理App自动化测试中遇到的Toast元素定位的方法。Toast在Web UI测试中也常见,通常作为轻量级反馈短暂显示。文章提供了两种定位Toast元素的技巧.
10 0
|
4天前
|
Java Maven 开发者
Java一分钟之-Maven项目管理工具使用
【5月更文挑战第15天】Maven是Java开发的项目管理工具,用于自动化构建、依赖管理和项目信息管理。通过POM模型管理项目,依赖中央仓库。基本目录包括`src/main/java`、`src/main/resources`、`src/test/java`和`src/test/resources`。常用命令有`clean`、`compile`、`test`、`package`和`install`。面对依赖冲突、找不到依赖或编译错误,可以调整`pom.xml`或`settings.xml`。理解Maven的工作原理和解决常见问题能提升开发效率。
17 0
|
4天前
|
存储 Java Maven
Maven 构建 Java 项目
使用 Maven 的 `maven-archetype-quickstart` 插件在 `C:\MVN` 创建 Java 应用项目 `consumerBanking`,命令行参数包括 `-DgroupId`, `-DartifactId` 和 `-DarchetypeArtifactId`。项目包含 src/main/java 和 src/test/java 目录,分别存放 Java 代码和测试代码,以及 src/main/resources 用于存储资源文件。默认生成的 `App.java` 和 `AppTest.java` 分别为应用主类和测试类。
|
4天前
|
Web App开发 缓存 前端开发
《手把手教你》系列技巧篇(四十四)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-下篇(详解教程)
【5月更文挑战第8天】这篇文档介绍了如何在IE、Chrome和Firefox浏览器中处理不信任证书的问题。作者北京-宏哥分享了如何通过编程方式跳过浏览器的证书警告,直接访问不受信任的HTTPS网站。文章分为几个部分,首先简要介绍了问题背景,然后详细讲解了在Chrome浏览器中的两种方法,包括代码设计和运行效果,并给出了其他浏览器的相关信息和参考资料。最后,作者总结了处理此类问题的一些通用技巧。
16 2
|
4天前
|
Java Android开发
【Java开发指南 | 第十八篇】Eclipse安装教程
【Java开发指南 | 第十八篇】Eclipse安装教程
12 2
|
4天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-上篇(详解教程)
【5月更文挑战第7天】本文介绍了如何在Java+Selenium自动化测试中处理浏览器对不信任证书的处理方法,特别是针对IE、Chrome和Firefox浏览器。在某些情况下,访问HTTPS网站时会遇到证书不可信的警告,但可以通过编程方式跳过这些警告。
13 1

热门文章

最新文章

推荐镜像

更多