How to include JavaParser Dependencies with Maven? - java

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>

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.

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.

Related

IntelliJ can't find google-cloud-storage classes

I'm trying to use Google cloud storage and I'm following this guide: https://cloud.google.com/storage/docs/reference/libraries#using_the_client_library
I'm using Java with Maven, and this is my Pom.xml:
<?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>com.findwise.app</groupId>
<artifactId>data-pipeline</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.32.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.cloud.dataflow</groupId>
<artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.70.0</version>
</dependency>
</dependencies>
</project>
As you can see I've added the google-cloud-storage dependency. My problem is that the classes used in the Java part of the guide (Storage, StorageOptions, Bucket, BucketInfo) are unfindable by IntelliJ. I can't import them. In fact, when I try to import via the absolute path:
import com.google.cloud.storage.Storage;
IntelliJ complains that the symbol google cannot be resolved. What is wrong here?
There are a few places to look:
Find your local .m2 repository and navigate to the com.google.cloud dependencies. If you don't find it, or the JARs aren't there, IntelliJ won't be able to access them.
IntelliJ will show you the External Libraries it is using under your source code in the project panel on the left.
If IntelliJ cannot import the class, it suggests that your Maven pull isn't working.

How to resolve import in Eclipse?

Im new in Java development and not familiar with various kinds of import (Maven, Git, etc), so I make it simple:
import com.google.common.collect.*;
import com.google.gson.*;
These two is not resolved in code Im inspecting, and I have no idea what kind of actions I should take neither what I should import to resolve it but it is probably some popular library.
Is there complete guide how developers import packages in eclipse (for example C# developers use Nuget, despite there is a ton of hand made ones), or they really use all this enormous import selector?
First of all Mavenise your current project and add the following dependency to it:
Goto: POM.XML after converting your current project to Maven project.
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
------Properties Here----
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0-rc2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
--------Add your Dependencies here, remember you have to add dependencies under <dependencies> here </dependencies> ----------
</dependencies>
</project>
Search for all dependencies here: https://mvnrepository.com/ , incase you need more dependencies to import.
How to mavenise your current Java Project:
In your eclipse just right click on Java Project and click Configure and you should see “ Convert to Maven Project ” option.
What is POM.XML
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Now, you can add a dependency in pom.xml.
If you use no dependency management tool like maven the simplest way is just download corresponding JARs and add them manually: http://www.oxfordmathcenter.com/drupal7/node/44
For GSON use this: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1
For the second dependency I suppose you should use GUAVA:
https://github.com/google/guava/wiki/UseGuavaInYourBuild
or this: https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2
But actually better to convert your project to Maven project (as described for example here: https://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/) so that you'll be able to use pom.xml for dependency management or Gradle (see configuring existing eclipse java project to build using gradle) and avoid manual JAR download

Basic maven question: Does maven transitively install dependencies?

I've looked here https://blog.packagecloud.io/eng/2017/03/09/how-does-a-maven-repository-work/ and that does seem to be the case.
However, I tried to experiment with mvn install and I'm not sure if it's worked as expected. Here's what I did
(1) I created a lib.
(2) Ran mvn install from the command line
(3) Copied the path of my newly created jar
(4) Opened a new maven project, stuck the path into my pom.xml
I'm able to reuse my library methods, BUT: one of my library methods returns a TransportClient which is part of the elasticsearch api. Using intellij inside my new project, it seems like I don't have elasticsearch even though I'm referencing the jar.
Is this expected? I was expecting it to have transitively installed elasticsearch when it referenced my jar.
I'd love a pointer or two in the right direction, I'm completely new to this. :)
My pom.xml for the lib that uses elasticsearch as 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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>estutorial</groupId>
<artifactId>estutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
</project>
My pom.xml for the new maven project that tries to reference the lib for the above pom.xml:
<?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>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<groupId>sth</groupId>
<artifactId>sth</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>estutorial</groupId>
<artifactId>estutorial</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/home/dell/.m2/repository/estutorial/estutorial/1.0-SNAPSHOT/estutorial-1.0-SNAPSHOT.jar
</systemPath>
</dependency>
</dependencies>
</project>
So, if I understand your steps, your dependency declaration in your referencing application uses a direct classpath to the jar file in your local repository? If so, this is unusual. You shouldn't need to know direct file locations for any of your dependencies of a Maven project. What you should be doing.
In the referenced project (that which requires the Elasticsearch library), it's pom.xml file would defined the elasticsearch dependency itself. This should follow maven standards for dependency declaration (groupId, artifactId and artifactVerion). If you don't have the elasticsearch artifact, maven will attempt to find it and store it in your local repository. You shouldn't have to have any path in your pom.xml file.
When you install the referenced project, it will install into your local repostory both the JAR file and the pom.xml.
In the referencing project, you should define the dependency to your referenced artifact in it's pom file. Same format: groupId, artifactId and artifactVersion. You shouldn't need to provide a specific path. What maven will do is find your referenced jar, but also use the installed POM.xml file for the referenced jar to find the transitive dependencies and include them in your classpath.
From what you've described, your dependency declarations aren't correct. If you can provide your POM file more details can be provided. Otherwise, review the maven intro to dependencies.
No. mvn install is a nearly useless command. It stuffs a jar file into your local repository, for subsequent use by other maven builds. You use the term 'path'. If you run mvn install:install-file to put a jar into your local repo under some coordinates, you can reference those coordinates from another pom; but it will generally lead to future problems as compare to deploying the jar into a proper repository manager.

maven build error: package org.apache.http does not exist

I am attempting to send a simple HTTP post from a java program (for deploying on Heroku).
I started with the demo project here.
Using mvn package builds the project successfully.
I then added my own additional file TestPost.java with a few lines of code, added it to the pom.xml, and still built fine.
Then I tried to add the HTTP code from this example (minus the package line), which uses the Apache HttpClient library.
Using mvn package results in the following error:
package org.apache.http does not exist
After searching for solutions I tried including a dependency in the pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
My understanding was that this should force a download of the necessary package but no download was shown on the next compile (just the same error), and the package is not visible in my user .m2\repository\ folder.
I tried inserting this dependency at different points in my pom.xml without success.
Why is the apache library not being downloaded? Please note that I am new to maven.
Here is the pom.xml that you should have if indeed you need to depend on httpclient.
<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>demo</groupId>
<artifactId>httpclient-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>httpclient-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</project>
Now if you put your Java sources in src/main/java, where src and pom.xml are in the same directory, Maven should resolve the dependency from your local repository, and download it, should it not already be there. Your local repository is defined in the conf/settings.xml in your Maven installation directory.

How do I add the storm mongo library as a maven dependency?

I'm trying to use a SimpleMongoBolt from storm-contrib. I downloaded the source, entered the storm-contrib-mongo directory and ran mvn package and mvn install. Everything worked fine and IntelliJ was able to resolve things while coding. When I try to build my project, however, it tries to find a pom for this library on an external repository. When it can't find it, it fails. What do I need to do to fix this?
<?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>StormTest</groupId>
<artifactId>StormTest</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>StormTest</name>
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>storm</groupId>
<artifactId>storm</artifactId>
<version>0.7.2</version>
<scope>Test</scope>
</dependency>
<dependency>
<groupId>com.rapportive</groupId>
<artifactId>storm-amqp-spout</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.rapportive</groupId>
<artifactId>storm-json</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>storm</groupId>
<artifactId>storm-contrib-mongo</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
You can try to check if you have proper dependencies defined in your project pom and compare them to storm artifact one. groupId, artifactId and version must be the same or Maven will try to download it from external repository and probably failed as it has never been installed on any public Maven repo.
When you install your artifact, it goes to user-directory/.m2/repostiory/group/id/path/*artifact/id/path*/version.
For your storm-amqp-spout you should have it in:
user-directory/.m2/repository/com/rapportive/storm-amqp-spout/0.1.1 folder.
There you should have few files:
jar itself (if it was packaged as jar file).
pom.xml file (the same you created for your project and you used to built and install it).
optionally sha1 files for both above.
If you don't have them, you probably made some mistake installing the artifact into repository. You can try to install it again or manually create pom just copying it from artifact source directory.
If there's correct pom.xml, I don't really have idea as I have never worked with IntelliJ (idea? ;)).
SimpleMongoBolt in Storm-Contrib was actually out of date. I updated the module myself and submitted a pull request, which has yet to be merged. Currently you can retrieve the updated code from my Storm-Contrib fork.

Categories