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
Related
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.
I'm working on a project where I have a "core" and an "API" that are two separate maven projects.
Currently, I'm exporting the API in a jar using Eclipse and I'm linking it in maven like that :
<dependency>
<groupId>com.project.my</groupId>
<artifactId>project-api</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/dependencies/project-api.jar</systemPath>
</dependency>
It's working properly and I can use the classes defined in my API.
But now I want to have a better workflow and I'm trying to install the API in my local maven repository and then link it normaly in the core :
<dependency>
<groupId>com.project.my</groupId>
<artifactId>project-api</artifactId>
<version>1.0.0</version>
</dependency>
But when I run maven clean install I'm getting the following error :
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:repackage failed: Unable to find main class
And I indeed don't have a main in my API, since I don't need one.
Note that I use spring-boot for both projects, as I'm using some of their dependencies and it's easier to let them manage the versions (I only use the utility classes in the API).
So I'd like to know if I HAVE to have a main anyway, even if I don't ever use it, or if I'm doing something wrong trying to install my API in the local repository ?
Thanks :)
Thanks to Thilo's wise advices, I managed to get it working.
So what I did was removing all unused spring-boot references from my API project and only keeping the spring dependencies I needed, and I was then able to run mvn clean install properly.
Here's what I got rid of :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
and
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/>
</parent>
I had to add the versions to some of the dependencies that were previously managed by spring-boot, but it's now working fine.
I am having trouble importing spring framework into eclipse. I have downloaded spring but am unable to import it. Can anyone help or direct me to a web page that can do so?
You can either follow this tutorial:
http://www.tutorialspoint.com/spring/spring_hello_world_example.htm
or use the spring eclipse plugin (you can find it in eclipse under Help/eclipse marketplace and search for it)
or you can use a tool to manage your dependencies like maven.
It wouldn't be a bad idea to get familiar with a dependency manager like Maven. For use in Eclipse there is a nice plugin M2Eclipse. The pom.xml file is what you use to specify your dependencies, and then when you build with Maven it resolves everything for you and downloads automatically anything it needs. This is the example pom.xml file from the Maven installation instructions:
<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>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The dependency for the core Spring Framework is
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
And generally you can find the pom.xml dependency specifications at the Maven Repository. Might seem a little overdone for simply getting Spring into Eclipse, but once you have it set up it is then a snap to add more dependencies down the road, all you do is add the appropriate item to the pom.xml file.
Currently I have a Gradle build for a Java Web Start Swing application that works. It's mainly using the steps outlined in: http://www.apprenticeshipnotes.org/2013/01/generating-webstart-files-using-gradle.html
The huge issue with doing a Gradle build this way, is that we are wasting time signing unchanging JARs every time we do a build (by far where most time is spent) in order to build the application.
I've been looking at Gradle code on Github, looking at other recipes that might point me in the right direction, but I can't seem to determine how I would go from a set of tasks that takes the build artifacts of an existing configuration, signs them, and then bundles them into the application to something that is much more efficient.
My idea was to have the ability to resolve dependencies, then determine if there was a 'signed' version of that dependency in the repository. If there was not a signed dependency, the build would sign that artifact and upload to a repository.
An easier approach that I was trying to take was a project that takes the runtime configuration of the application project and somehow loops over the dependencies and signs them and uploads the artifacts to a repo, but it's not clear to me how I could do this with the Configuration API. Just getting at the files of the configuration is not enough, because I would want dependency information for each JAR file.
It also gets a bit trickier with uploading the JARs, because if, for example, I upload a signed jar, say metrics-json-3.0.2.jar, as metrics-json-signed-3.0.2.jar, I would need to update the POM to reference the signed artifacts. Seems like there should be an easier approach
<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>
<parent>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-parent</artifactId>
<version>3.0.2</version>
</parent>
<artifactId>metrics-json</artifactId>
<name>Jackson Integration for Metrics</name>
<packaging>bundle</packaging>
<description>
A set of Jackson modules which provide serializers for most Metrics classes.
</description>
<dependencies>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>
Note, this is different from the signing mechanism provided by the existing Signing Plugin:
http://www.gradle.org/docs/current/userguide/signing_plugin.html
I found that Maven implies specific directory layout. But I don't understand from here: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
where java libraries needed to compile and run my code should be placed. I think they shouldn't be placed under 'src/main/resources' because resources is something like images or so. Also it doesn't look right to place them under 'src/main/java'. If I wouldn't use maven, I'd place libraries in project's root lib directory. But I don't think that for maven project it will be right. Please advise.
UPD: I solved the problem. The matter was that I set packages for my sources as src.main.myApp instead of main.myApp. This seems to upset maven.
Maven handles your project dependencies in a different way to a 'Standard' Java project.
You declare the libraries you depend on in your project's pom.xml:
e.g.
<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>org.example</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>your-project-web</name>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</project>
When you use a maven command to build the project, i.e. mvn install, it will download the dependencies for you and store them in your local repository.
In Maven, you do not keep libraries in your project. You specify dependencies on these libs, and they get populated into your local repository. At the time of build, if you are packaging the libs (say for a war file), they do get pulled into target//WEB-INF/lib. But in general, the whole idea is not to deal with these libraries or manage them, just to manage dependencies in your pom file, and forget the rest.