Translation: Apache Maven - Simplified Java Construction Process - More (3) Integrated Unit Test than Apache Ant (3)

xiaoxiao2021-03-06  19

Integrated unit test

The only thing you have to do is telling Maven to find test code there, and the rest of the Maven is automatically completed by Maven:

1. In the project object model file Project.xml, add the UnitTestSourceDirectory element by the SourceDirectory element.

...

$ {Basedir} / src / test

...

2. Create a test code called BoxTest:

file name:

C: /daven/src/java/smartsoft/daven/boxtest.java

content:

Package smartsoft.daven;

Import junit.framework.testcase;

Public Class Boxtest Extends Testcase {

Public void test1 () {

Box b = new box (2, 2);

Assertequals (4, B.Getarea ());

}

}

3. Run the Java: JAR target, it will indirect call Java: Compile, Test: Test two goals

Maven Java: Jar

The output of the console is as follows:

C: / daven> maven jar

Java: Prepare-FileSystem:

Java: Compile:

[echo] compling to c: / daven / target / classes

Java: Jar-Resources:

Test: prepare-filesystem:

Test: Test-Resources:

Test: Compile:

[Javac] Compiling 1 Source File to C: / Daven / Target / Test-Classes

Test: Test:

[JUnit] Dir Attribute Ignored IF Running Same VM

[junit] Running SmartSoft.daven.boxtest

[JUnit] Tests Run: 1, Failures: 0, Errors: 0

Build Successful

Total Time: 10 Seconds

Resource library organization JAR file

Maven's repository Organizes Jar Files

Most projects use third-party libraries in the form of jar files. The repository is a special Maven mechanism for organizing the jar files and other dependencies that your builds use. (Maven also uses the term artifact to refer to dependencies.) The repository essentially is a folder with a Maven-specified structure, and it solves two problems. First, it provides a central location for all the jar files and other build dependencies you need to build your projects. Second, it helps deal with version issues by proposing a Naming Convention. The Following Is The Structure of a Maven Repository:

/ repository

JDO

Jars

Kodo-1.1.jar

Kodo-1.2.jar

JDointerfaces-1.0.jar

Filters

Jars

JXFilter-1.0.jar

JXFilter-1.1.jar

OR, more generally:

/Repository/groupid/type/artifactid-version.ext

Thus, for kodo-1.1.jar, jdo is the groupid, jar is the Type of Dependency, Kodo is the artifactid,

And 1.1 is the version. These elements will be represented in your pom.

Maven supports a local repository and a shared network repository. It first checks the local repository,

And if it doesn't find the jar file, it Checks the shared repository. maven caches jar files from the

Shared Directory in The Local Directory.

DETERMINING Project Dependencies

Maven's repository feature isclosely related to the Way Maven Deals with Project Dependencies.

In ant, you have to create a classpath to indicate Which Jar Files your project Depends ON,

While Maven Uses The Dependencies Element in Project.xml for That Purpose.

Add A Jar File Dependency to your project to see how this works. Add The Following Block of

Code to Project.xml, Below The CurrentVersion Element But Above The Build Element:

...

1.0

JDO

kodo

2.5.2

...

In Order for this Tag to Work, The Following Jar File Must EXIST:

/Repository/jdo/jars/kodo-2.5.2.jar

Multiple Goals Use The Dependency Tag. For Example, The Compile Goal Uses It to generate

The ClassPath, And The Deploy Goal Uses It To Determine Which Jar Files To Copy.

转载请注明原文地址:https://www.9cbs.com/read-53576.html

New Post(0)