如何为在Maven中设置的属性下声明的属性设置类路径

如何为在Maven中设置的属性下声明的属性设置类路径

本文介绍了如何为在Maven中设置的属性下声明的属性设置类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在致力于从ant build工具转换为maven工具. Ant build.xml通过以下方式初始化了属性

I am working on converting from ant build tool to maven tool. Ant build.xml has initialized properties in below way

<property name="home.dir" value="${basedir}"/>
<property name="external.dir" value="${home.dir}/external"/>

并且已经在build.xml中设置了类路径,如下所示:

and class path has been set in build.xml as below:

<target name="setClassPath">
    <path id="classpath_jars">
        <fileset dir="${external.dir}/log4j" includes="*.jar"/>
    </path>
</target>

能帮我如何在pom.xml中添加类路径吗?

Could you please help me how to add classpath in pom.xml?

推荐答案

使用maven时,不必在意手动定义类路径.
在开始学习Maven时,您要内部化的最基本的事情之一是:Maven遵循基于配置的约定

Don't care about manually defining the classpath when using maven.
One of the most basic things you've to internalise when start learning Maven is: Maven follows the the concept convention over configuration

对于类路径,这意味着,您在pom.xml<dependencies>部分添加的每个库(maven术语是依赖项)都自动成为类路径的一部分.

For the classpath this means, that every library (the maven term is dependency) which you add in the section <dependencies> of pom.xml is automatically part of the classpath.

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.so</groupId>
    <artifactId>csvProject</artifactId>
    <version>1.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>2.3</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

如果需要配置文件或图像作为类路径的一部分,请将它们放在项目的resources文件夹中.

If you need a config file or an image to be part of the classpath, put them in the resources folder of your project.

一个典型的入门Maven项目如下所示:

A typical starter Maven project looks like this:

csvProject
|   pom.xml
|
+---src
|   +---main
|   |   +---java
|   |   |   \---de
|   |   |       \---so
|   |   |               CsvExample.java
|   |   |
|   |   \---resources
|   |   |   \---images
|   |   |   |         logo.png
|   |   |   |   some.properties
|   |   |   \---de
|   |           \---so
|   |                   more.properties
|   \---test
|       \---java

有关更多信息,请参见 Maven主页或使用Google查找教程.

For more informations look atMaven home or use google to find a tutorial.

这篇关于如何为在Maven中设置的属性下声明的属性设置类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:24