How to include JavaParser Dependencies with Maven?

2.3k views Asked by At

I read the official http://javaparser.org/ page but I don't know how to install it. I saw this answer https://stackoverflow.com/a/32215185/7643663 that told me to

easily create a project using Maven and including among the dependencies JavaParser

along with

<dependency>
    <groupId>com.github.javaparser</groupId>
    <artifactId>javaparser-core</artifactId>
    <version>2.2.1</version>
</dependency>

But I don't know how to build my code using javaparser with maven. I executed mvn clean install in the directory I downloaded the javaparser source code with no errors. Then I tried to run the following:

import com.github.javaparser.JavaParser.*;

public class PreprocessJavaparser {

    public static void listClasses() {
        CompilationUnit compilationUnit = JavaParser.parse("class A { }");
        ClassOrInterfaceDeclaration classA = compilationUnit.getClassByName("A");

    }

    public static void main(String[] args) {
        listClasses();
    }
}

But when I import com.github.javaparser.JavaParser.*; I get this error: package com.github.javaparser.JavaParser does not exist.
So I think I didn't install JavaParser correctly or I have to deal somehow with the JavaParser Dependencies in a pom.xml.

Here is my pom.xml with the javaparser-core dependency:

<?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>test</groupId>
    <artifactId>preprocess</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.github.javaparser</groupId>
                <artifactId>javaparser-core</artifactId>
                <version>3.3.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
2

There are 2 answers

0
Federico Tomassetti On BEST ANSWER

I executed mvn clean install in the directory I downloaded the javaparser source code with no errors.

No, you should not do that. The whole point of maven is that it takes the compiled version of a library (e.g. JavaParser), download it and let you use it in your own project. It means that unless you want to change JavaParser itself (you do not want to) you do not need to download the source code of JavaParser.

Just create your own project and add JavaParser as a dependency to that project. That is it.

2
lexicore On

Your import is wrong:

import com.github.javaparser.JavaParser.*;

You try to import from the JavaParser class as if it were a package.

Should be

import com.github.javaparser.JavaParser;

Or

import com.github.javaparser.*;

For all the classes/packages you use.

@WilliamReed I don't use an IDE. – AIpeter

Well, maybe you should, it will help you with this sort of problems.

Another problem is here:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.github.javaparser</groupId>
            <artifactId>javaparser-core</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

You should use dependencies, not dependencyManagement/dependencies. The latter only declares dependencies, does not actually use them.