Maven创建第一个java项目(官方教程)

翻译自:链接

Building Java Projects with Maven

This guide walks you through using Maven to build a simple Java
project.

使用Maven构建一个java项目?
这个指南使你通过使用Maven去创建一个简单的Java项目

What you’ll build

You’ll create an application that provides the time of day and then
build it with Maven.

你将要创建什么?
你将提供今天的时间,然后通过Maven去,创建一个应用程序

What you’ll need(你需要的东西)
About 15 minutes(大概15分钟)
A favorite text editor or IDE(一个喜欢的编辑器或者IDE(集成开发环境))
JDK 6 or later(JDK 6 或者更高的版本)

.

How to complete this guide
Like most Spring Getting Started guides,
you can start from scratch and complete each step, or you can bypass
basic setup steps that are already familiar to you. Either way, you
end up with working code.

如何完成这个指南?
像大部分Spring指南一样,你可以从头开始或者,完成每个部分,或者你可以绕开基本的设置步骤如果你已经很熟悉。不管怎样,你最终都需要进行编码工作

To start from scratch, move on to Set up the project.

To skip the basics, do the following:

1.Download and unzip the source repository for this guide, or clone it using Git: git clone https://github.com/spring-guides/gs-maven.git
2.cd into gs-maven/initial
3. Jump ahead to [initial].

从头开始,跳转到 建立项目。

跳过基本步骤,按照下面步骤:
1.下载或者从这个指南的字眼仓库中解压,或者克隆它使用
Git:git clone https://github.com/spring-guides/gs-maven.git
2.进去该目录,执行
gs-maven/instial
3.向前跳转去initial(项目名)

When you’re finished, you can check your results against the code in gs-maven/complete.

当你完成时,你可以选择你的结果通过 gs-maven/complete

Set up the project
First you’ll need to setup a Java project for Maven to build. To keep the focus on Maven, make the project as simple as possible for now.

建立项目
首先你将需要去通过Maven构建一个java项目,使得焦点在Maven上,使项目像现在一样简单。

Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems:

创建一个目录结构
在逆选择的项目目录中,创建下面子目录结构;
例如 使用 mkdir -p src/main/java/hello(在*nix系统中)

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

Within the src/main/java/hello directory, you can create any Java classes you want. To maintain consistency with the rest of this guide, create these two classes: HelloWorld.java and Greeter.java.

在”src/main/java/hello” 目录中,你可以创建任何你想要的java的类文件,最好和下面的指南一致,创建那两个类文件,HelloWorld.java 和 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!";
    }
}

Now that you have a project that is ready to be built with Maven, the next step is to install Maven.
Maven is downloadable as a zip file at http://maven.apache.org/download.cgi. Only the binaries are required, so look for the link to apache-maven-{version}-bin.zip or apache-maven-{version}-bin.tar.gz.
Once you have downloaded the zip file, unzip it to your computer. Then add the bin folder to your path.

现在你有一个通过Maven构建好的项目,先一步是 install(maven的一个命令) Maven

Maven是可以在这个网站下载zip版,http://maven.apache.org/download.cgi. 下载,只有这个文件是必须的。所以 查找这个链接,下载apache-maven-{version}-bin.zip(windows版本) or apache-maven-{version}-bin.tar.gz(linux版本)

你已经下载zip文件,把它解压到你的计算机。然后添加 bin 目录,到你的环境变量中。

To test the Maven installation, run mvn from the command-line:

测试maven安装,在命令行中运行:mvn

mvn -v

If all goes well, you should be presented with some information about the Maven installation. It will look similar to (although perhaps slightly different from) the following:

如果一切都顺利,你可以看到一些关于Maven安装的信息,它看起来和下面的很像(尽管有一些细微的区别)

Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 07:51:28-0600)
Maven home: /usr/share/maven
Java version: 1.7.0_09, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.3", arch: "x86_64", family: "mac"

Congratulations! You now have Maven installed.
祝贺你你现在可以运行Maven install

Define a simple Maven build
Now that Maven is installed, you need to create a Maven project definition. Maven projects are defined with an XML file named pom.xml. Among other things, this file gives the project’s name, version, and dependencies that it has on external libraries.

定义一个简单的Maven build
现在这个Maven被 install ,你需要去创建一个 Maven项目定义。Maven项目是被通过名叫pom。xml的xml文件定义的。在其他事情中,这个文件,提供项目的名称,版本,他已经继承库的依赖性

Create a file named pom.xml at the root of the project and give it the following contents:

在项目的根目录下,创建一个文件名叫pom.xml,并提供下面内容

pom.xml

<?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>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

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

With the exception of the optional packaging> element, this is the simplest possible pom.xml file necessary to build a Java project. It includes the following details of the project configuration:

使用可选择的packaging>标签之外的,这个最简单的 pom.xml 文件需要构建一个java项目。它导入下面的项目配置的细节

modelVersion pom 模型的版本 (总是 4.0.0).
groupId 项目所属的团队,通常表示成反写的域名
artifactId 用于区分库中项目的名称(例如,jar或者war文件的名字)
version 项目构建的版本
packaging 项目如何被打包. 默认为jar,打包成jar文件. 使用war可以打包成war文件

When it comes to choosing a versioning scheme, Spring recommends the semantic versioning approach.
当谈到选择版本时,Spring 推荐使用[语义版本](http://semver.org/lang/zh-CN/ )方法

At this point you have a minimal, yet capable Maven project defined.
在这你制定一个最低的,但是也可以使用Maven项目默认的。

.

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.
To try out the build, issue the following at the command line:

构建java代码
Maven 现在已经构建好了项目,你现在需要执行一些构建生命周期目标使用Maven,导入目标,去编译项目的代码,创建一个库包(例如 一个 jar文件),并且,安装lib在本地的Maven依赖仓库中。
尝试 去构建,在命令行中,发出下面在这条命令

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.
Since it’s unlikely that you’ll want to distribute or work with .class files directly, you’ll probably want to run the package goal instead:

这将会运行Maven,告诉他,执行编译目标。当他完成时,你可以找到编译后的class文件,在target/classes 目录中

你想发布或者和class文件一样是不可能的,你大概需要运行,package 目标代替

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.

这个打包目标将会编译你的java代码,运行所有的测试文件,并且完成后将代码打包成jar文件 放入 target 目录中,jar文件的饼子是以项目的artifactId> 和 version>为基准的
例如,大抱歉提供一个最低版本的pom.xml文件,jar文件将会命名为 gs-maven-0.1.0.jar

If you’ve changed the value of packaging> from “jar” to “war”, the result will be a WAR file within the target directory instead of a JAR file.

如果你把packaging>标签的值从jar改成war,结果将会是war文件,在target 目录而不是一个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:

Maven也维护一个在你的本机的依赖仓库(通常在 你的home目录下的 .m2/repository 目录),目的是快读存取项目的依赖。如果你install你的项目jar文件到这个本地仓库,然后,你需要调用 install 目标

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.

Speaking of dependencies, now it’s time to declare dependencies in the Maven build.

这个install 命令 将会会执行,compile,test和package 对你的项目源码,并且拷贝它到本地依赖仓库,准备作为一个依赖给其他项目参考它

说到依赖,现在说说在Maven构建时,定时声明依赖

Declare Dependencies

The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and complex functionality.

For example, suppose that in addition to saying “Hello World!”, you want the application to print the current date and time. While you could use the date and time facilities in the native Java libraries, you can make things more interesting by using the Joda Time libraries.

First, change HelloWorld.java to look like this:

声明依赖
一个简单的HelloWorld是一个完全独立并且没有依赖的附加库。然而,更多的应用程序依赖外部库区使用共同的或者复杂的函数。

例如,假如除此之外 saying “Hello World”,你想应用程序打印当前数去和时间。当你像在本地java库中使用数据和时间工具,你可以做一些更多有趣的事情通过使用 Joda Time 库

首先,改变HelloWorld.java 看这个

src/main/java/hello/HelloWorld.java

package hello;

import org.joda.time.LocalTime;

public class HelloWorld {
    public static void main(String[] args) {
        LocalTime currentTime = new LocalTime();
        System.out.println("The current local time is: " + currentTime);
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
    }
}

Here HelloWorld uses Joda Time’s LocalTime class to get and print the current time.

If you were to run mvn compile to build the project now, the build would fail because you’ve not declared Joda Time as a compile dependency in the build. You can fix that by adding the following lines to pom.xml (within the project element):

这HelloWorld 使用 JodaTime的LocalTime类去获取并打印当前的时间,

如果你运行 mvn compile 去构建这个项目,构建将会失败,因为在构建时,你还没有声明 Joda Time 作为编译的依赖。你可以将下面这几行添加到pom.xml文件中(在project标签中)

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

This block of XML declares a list of dependencies for the project. Specifically, it declares a single dependency for the Joda Time library. Within the dependency element, the dependency coordinates are defined by three sub-elements:

groupId - The group or organization that the dependency belongs to.
artifactId - The library that is required.
version - The specific version of the library that is required.
By default, all dependencies are scoped as compile dependencies. That is, they should be available at compile-time (and if you were building a WAR file, including in the /WEB-INF/libs folder of the WAR). Additionally, you may specify a scope> element to specify one of the following scopes:

provided - Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).
test - Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.
Now if you run mvn compile or mvn package, Maven should resolve the Joda Time dependency from the Maven Central repository and the build will be successful.

这块xml给项目声明了一组依赖。特别强调,他声明了一个单一的依赖,Joda Time库。在dependency>标签中,依赖坐标被定义通过三个标准标签。

groupId> - 依赖所属的组织或组
artifactId> - 这个是必须的,标志库
version> - 这个是必须的,库的版本

默认是,所有的依赖都会被作为编译的依赖。他们应该被提供作为编译时(如果 你构建一个war文件,导入war到/WEB-INF/libs 文件)。除此之外,你需要制定一个scope>标签,去指定一个下面的范围

provided - 依赖项目编译所需的代码,但这将在运行时通过提供一个容器运行代码(比如java Servlet 的API).
test - 依赖项用于编译和运行测试,但不是必需的建设或运行项目的运行时代码。

如果你运行mvn编译或者mvn包,Maven应该解决Joda Time依赖从Maven中央存储库和构建会成功。

Write a Test

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

首先添加JUnit pom作为依赖项。xml,在测试范围:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</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 uses a plugin called “surefire” to run unit tests. The default configuration of this plugin compiles and runs all classes in src/test/java with a name matching *Test. You can run the tests on the command line like this

Maven使用一个插件称为“surefire”运行单元测试。这个插件的默认配置编译和运行所有类在src/test/javawith名称匹配*Test。您可以在命令行上运行测试

mvn test

or just use mvn install step as we already showed above (there is a lifecycle definition where “test” is included as a stage in “install”).

Here’s the completed pom.xml file:

或者只是使用mvn install步骤如上我们已经显示(有一个生命周期定义在“test”是包含在“install”阶段)。

这是 完整的 pom.xml 文件

pom.xml

<?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>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <dependencies>
        <!-- tag::joda[] -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- end::joda[] -->
        <!-- tag::junit[] -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- end::junit[] -->
    </dependencies>

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

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.

完整的pom.xml 文件可以Maven shade 插件简单钢鞭的构建可执行的jar文件。这个指南的重点是开始使用Maven,这个特定的例子不使用这个插件

Summary

Congratulations! You’ve created a simple yet effective Maven project definition for building Java projects.

Want to write a new guide or contribute to an existing one? Check out our contribution guidelines.

总结
恭喜你!您已经创建了一个简单但有效的Maven项目定义构建Java项目。

想写一个新的指南或导致现有的吗?看看我们贡献的指导方针。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,可以创建一个绘制图类并实现继承。继承是面向对象编程的一项重要特性,它允许我们基于现有的类创建新的类,同时继承其属性和方法。绘制图类的继承可以让我们在基础类的基础上添加更多的功能或者进行修改。 首先,我们需要创建一个基础的绘制图类,该类包含绘制图形的基本方法和属性。这个基础类可以包含一些通用的绘制图形的方法,比如画圆、画矩形等等。这些方法可以用来在继承类中直接调用。 接着,我们可以创建一个继承自基础绘制图类的新类。这个新类可以添加一些特定的绘制图形的方法或者覆盖基础类的方法来实现自定义的绘制行为。通过继承,我们可以复用基础类的代码,并在此基础上实现新的功能。 例如,我们可以创建一个名为"Circle"的类继承自绘制图类。在这个类中,我们可以添加一个绘制圆的方法,并用圆的特定属性覆盖基础类的绘制方法。这样,我们就可以在"Circle"类中专注于圆的绘制逻辑,而不需要重新实现一些基本的绘制方法。 继承还可以构建一个类的继承层次结构,通过创建多个继承类可以实现更复杂的绘制图形功能。比如,我们可以创建一个"Rectangle"类继承自基础绘制图类,然后再创建一个"Square"类继承自"Rectangle"类。这样,我们可以通过继承层次结构实现矩形和正方形的绘制,并复用之前继承类的功能。 总结来说,Java创建绘制图类的继承可以通过创建一个基础绘制图类,并在其基础上创建新的继承类来实现。这样可以提高代码的复用性和可维护性,同时支持定制化和扩展性的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鼠晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值