In what directory to put java libraries for maven project? - java

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.

Related

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

How can I create Servlet project with maven in Intellij Idea?

I have created a maven project in Intellij Idea. I also added jetty's maven dependency to pom file.
What should I do then to create servlet project?
Should I create webapp\WEB-INF\web.xml folder in main\java manually? or there is some maven plugins which I must add to Pom.xml file and after clicking install in Lifecycle, related folders and files will be created automatically?
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.c.helloWorldMavenServlet</groupId>
<artifactId>helloWorldMavenServlet</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I recommend to use maven archetype to create project:
org.apache.maven.archetypes:maven-archetype-webapp
Then you should create directory java in src/main,then right click-> Mark Directory As -> Sources Root.
By default, you have web.xml in version 2.3 so that's mean you cannot user CDI for example.
If you want to change it go to File->Project Structure->Facets(or Modules) and then delete Web Descriptor from Deployment Descriptors and then add new web.xml(+) and choose version 3.1.
I hope it should work. At this moment I don't know any other way to achieve that

How to use Json library in web application

I am working on a web application in JAVA, and I used org.json. in the project for some computation, but I get the following error message when I run the application from localhost.
java.lang.NoClassDefFoundError: org/json/JSONObject
I specified the dependency in 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloudera.oryx</groupId>
<artifactId>projectname</artifactId>
<version>0.5.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
</dependencyManagement>
EDITED: Full Pom.xml content
What other thing should be done to get this to running? Thanks.
There may be various reasons, you get NoClassDefFoundError even after adding the dependency
The following are the checks you can do to get.
Add the dependency in <dependencies> section of your main pom.xml. If you feel like I dont require JSON library in service layer in an application like Rest --> service --> DAO, then add in <dependencyManagement> section and add the dependency with out version - in which ever module you want.
You need to make sure that your settings.xml is configured right enough to get the jar files downloaded for you.
Another possibility is refresh your workspace if you are using some IDE - eclipse, IntelliJ to get the dependencies updated.
You can check .m2 --> repository --> org --> json --> <version> folder is available or not. This confirms that the jar is downloaded and you can tick point 2 as confirmed.
You can unarchive the war file and check in the libraries whether the json jar file is added to your end packaging or not. This is the final place to check.
You can use mvn -U clean install to update the dependencies.
I think the problem is your POM's "packaging"
The normal way to build a webapp is to specify the packaging as "war" so that Maven will build a WAR file containing your code and the dependent JARs. You then deploy the WAR file to the web container.
But you are using packaging "pom".
I'm assuming that means you've got another (child) POM file to build your application JAR.
I'm also guessing that you are using "jar" as the packaging in that POM.
I'm also assuming that you are then deploying the JAR to web container; e.g. by hand or using some IDE integration thingy.
I think that the problem here is that while you are deploying your JAR file to the web container, and you don't need to deploy the Jersey dependencies ('cos they are already there!!), you are NOT deploying the "json.org" JAR file.
At any rate, the reason that you are getting the exception is that the web container classloader cannot find that JAR file. If you are deploying without using a WAR, something has to copy the file to the place where the web container's classloader is going to look.

Download Maven Repository files

I have a Maven repository that has jar files I want to use locally on other projects. What is the best way to download those jar files from the Maven repo.
I went to http://mvnrepository.com/ but the jar files do not work in my build. I would like to have is the Maven build jar files, but I am not able to save them from my project in Netbeans.
Is there a way to download all the files within Maven repository?
There's probably thousands of jar files in maven central, so I wouldn't try to download all of them.
Generally, if you want to use jars found in maven repositories, you may want to start a maven project yourself; configure your pom.xml to require those dependencies, and they'll be downloaded automatically.
Most java IDEs have maven support or a maven plugin.
-- EDIT --
Here's a really quick pom.xml example from maven's website:
<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>
If you're using eclipse, you'll probably want to m2e plugin to handle most of this for you. Plus, it will link the javadoc to those jars as well. :)
Why don't you try using Maven instead? With Maven you're able to automatically manage your dependencies easily.
It's really simple. Please check this Maven in 5 minutes so you can begin with it successfully. Any doubts, please let me know.
-- EDIT --
As soon as you learn some main concepts, like dependencies, you can simple start your projects with the following command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
After that, you can simply import to the most used IDEs like Eclipse, using:
mvn eclipse:eclipse
The tool mvn2get (https://github.com/groboclown/mvn2get) will download published Maven artifacts suitable for use in a local repository. This includes the POM files, checksum files, and signature files, as well as able to search through dependencies.
It allows for a one-line execution to download the files:
$ mvn2get.py -d my-local-repo-dir --resolve log4j:log4j:1.2.17
I wrote this script based on a similar need to download into a local repository.
The obvious answer is to suggest that you consider building your code using Maven. This will give you native support for the Maven Central repository.
But... I sense that you just want to download the files you need to a local directory?
In that case I'd suggest using the Apache ivy command-line.
Example
The files you want are listed in a ivy.xml file. For example:
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="com.myspotontheweb" module="demo"/>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.6" conf="default"/>
<dependency org="junit" name="junit" rev="4.10" conf="default"/>
</dependencies>
</ivy-module>
And ivy can populate a local "lib" directory as follows:
java -jar ivy.jar -retrieve "lib/[artifact].[ext]" -ivy ivy.xml
The advantage of this approach is that ivy can download the additional transitive dependencies of the modules you've specified:
$ find lib -type f
lib/commons-lang.jar
lib/junit.jar
lib/hamcrest-core.jar
Note:
hamcrest-core is a depedency of junit.

Missing class in Maven project using Exchange web services

I've installed the following maven module to my local maven repository
EWS Maven Module but the IDE (Eclipse in this case) doesn't seem to know about the following class `GetUserAvailabilityRequest.
Interestingly, I can run a mvn compile on my project that uses the dependency above successfully.
I can even see the class file in the jar file under my Maven Dependencies in Eclipse.
Does anyone know why this might be occurring ?
Edit #tolitius:
ews-java is a dependency for my project, not the project I'm trying to import
Here is the pom file in question:
<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.conf</groupId>
<artifactId>conferenceclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>microsoft.exchange</groupId>
<artifactId>exchange-ws-api</artifactId>
<version>1.1.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Turns out that the class in question is not public. It had nothing to do with Maven or Eclipse or anything.I wasn't looking at the class since I didn't have the source attachment.

Categories