user
user

Reputation: 51

How to set classpath for properties declared under property set in maven

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

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>

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

Upvotes: 2

Views: 6581

Answers (2)

JimHawkins
JimHawkins

Reputation: 4998

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

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

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>

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

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

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

Upvotes: 2

Fahad Siddiqui
Fahad Siddiqui

Reputation: 1849

You can add custom classpath using additionalClasspathElement tag in pom.xml.

<additionalClasspathElement>${external.dir}/log4j</additionalClasspathElement>

Upvotes: 3

Related Questions