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