Adding a war dependency with appassembler-maven-plugin - java

I am using appassembler-maven-plugin to create the script to run an application. That application depends on several projects and solves this dependencies using Maven. One of the project is packeged as a war.
<dependency>
<groupId>my.war.dependency</groupId>
<artifactId>project</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
The problem I have is that the appassembler is not extracting the war dependency, only the jar dependencies. So I cannot refer to the classes in this war in order to create the script calling to one of the classes inside.
Can you help me?
Thanks in advance.

Related

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 project via Maven

All I want is the project file/jar for this project: http://thiagolocatelli.github.io/parse4j/
It says I need to do the following to obtain it:
Getting Started
Download the library manually
Maven
<project ...>
...
<dependencies>
<dependency>
<groupId>com.github.thiagolocatelli</groupId>
<artifactId>parse4j</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
...
</project>
I have never used Maven, do not really know what it is. Can someone advice me how I obtain the project file?
Maven is a dependency manager. Lots of information about it if you're interested - just use your favorite search engine.
You can also download the jar file directly from http://search.maven.org/remotecontent?filepath=com/github/thiagolocatelli/parse4j/1.3/parse4j-1.3.jar

Manually installed maven dependencies not visible in Eclipse import

I have installed maven dependency using the command below
mvn install:install-file -Dfile=d2_lib.war -DgroupId=com.emc.d2fs -DartifactId=d2_lib -Dversion=1.0 -Dpackaging=war -DlocalRepositoryPath="C:\Users\kumarr23\.m2\repository"
I have added this dependency in my root pom.xml as below
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
I can see when I do maven install the jars inside my final war,
when i try importing these classes from eclipse these classes are not included, Is there some thing I am missing?
I am new to maven and i am stuck here !
please advise
Please check if you have d2_lib.war file at the location from where you are running command or else mention the full path of d2_lib.war in command and try again , i believe with the command you specify it is not copying the war file since war file path is not defined properly , you can confirm this by checking if war file is copied to your local maven repository
mvn install:install-file -Dfile=d2_lib.war -DgroupId=com.emc.d2fs -DartifactId=d2_lib -Dversion=1.0 -Dpackaging=war -DlocalRepositoryPath="C:\Users\kumarr23.m2\repository"
There is a convention in maven that java code lives in a .jar file and web classes live in a .war file. When you include a dependency with the war type in your web project, maven will treat the included file as an overlay. This basically means that maven will package the contents of the dependency into your war file, however it will not include the libraries in that war as java code the way you are looking for.
When a war file is created, if the creator also wants people to get at the java code and transitive dependencies the code is based off of, they can use the maven-war-plugin to set a configuration tag of <attachClasses>true</attachClasses>. This will generate an additional artifact in the form of artifactId-version-classes.jar, along with the normal artifactId-version.war file.
When you include both the war as an overlay and the classes jar as a normal dependency, you will be able to see in your dependency tree that maven is only pulling in transitive dependencies from the jar file dependency, not the war.
In your case, you need to find the classes jar file and install it along with the war file in your local maven repository. Then you can add the classes jar to your pom.xml like so:
old pom.xml
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
new pom.xml
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<classifier>classes</classifier>
</dependency>

specifying dependency in maven

I have a jar file present in location
/path/to/foo2011020.jar
I am importing this jar as
import org.foo.FOOException;
import org.foo.FOOObject;
How do i specify this in my maven build file??
Is there any good tutorial for learning maven.
Will there be two instances in build file
I have tried various permutations and combinations but it is just not working. I bet this is something fairly straightforward.
Any pointers..
Thanks
if you don't want to publish the jar to some private/public nexus and you just want to have it on your disk then use <system> scoped dependency
for example:
<project>
...
<dependencies>
<dependency>
<groupId>your.group.id</groupId>
<artifactId>your-artifact.id</artifactId>
<version>your.version</version>
<scope>system</scope>
<systemPath>/path/to/foo2011020.jar</systemPath>
</dependency>
</dependencies>
...
</project>

Maven, package does not exist

I have a module whose pom file is:
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>common module</name>
In that artifact ('common'), I have a package named com.mycompany.common.objects. In the consuming package, my pom file is:
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
When I run mvn install it always complain: package com.mycompany.common.objects does not exist.
I tried explicit importing in the class where the error was:
import com.mycompany.common.objects
No luck. I tried in both the IDE (IntelliJ) and in commandline. Any idea? thanks
While working with IntellijIDEA, generated files can cause this issue. Writing
mvn idea:idea
on IntellijIDEA Maven console to reset those files did the trick for me. Also, see:
Package doesn't exist error in intelliJ
From your sample, we cannot see any artifact containing the package com.mycompany.common.objects you are using.
You are adding dependency com.mycompany.Common:common as a POM (and you are declaring the packaging of com.mycompany.Common:common as POM too). If it is actually a JAR artifact that contains the package you need to use, then remove the packaging from the POM and dependency (which means, using default which is JAR).
For anyone struggling with this and not familiar with java, make sure that the said package exists in your local repository. Maven has a local repository ~/.m2 where the packages are installed for local access, so even if your dependency package is correctly declared as a dependency in pom.xml and is compiled and exists in your project, if it does not exist in the local repository, the mvn compile will trigger a "package does not exist" error.
To fix this:
In the missing package folder, do:
mvn install //--> this will package and install your missing package in the local repo
Then in your project that you wanted to compile:
mvn compile // --> now that the missing package is in the local repo it should work
Please correct me, If I'm wrong. I understand that the common is a POM that defines several dependencies which intents to be used by other modules. The Importing Dependencies may meet your requirement.
For example
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I hope this may help.
I had the same problem recently. Everything in my project was setup correctly with dependencies etc. I tried removing /target dirs but nothing worked.
Finally, I solved it by removing the Module Dependency from my dependent project and then readding the dependency. Not sure what is going on in the background, but some sort of refresh of the classpath must have been made. Perhaps the problem was due to the Maven setup.
Hope it helps someone who reaches this question from a search engine.
Not sure if there was file corruption or what, but after confirming proper pom configuration I was able to resolve this issue by deleting the jar from my local m2 repository, forcing Maven to download it again when I ran the tests.
For me the problem was with the sourceDirectory and testSourceDirectory nodes in my pom.xml.
I was using
<sourceDirectory>${basedir}/src/test</sourceDirectory>
<testSourceDirectory>${basedir}/test</testSourceDirectory>
and changed it to
<sourceDirectory>../src/test/java</sourceDirectory>
<testSourceDirectory>../src/test/java</testSourceDirectory>
Your IDE (Eclipse in my case) may not distinguish between compile and runtime scope. This means that the IDE will let you use runtime scope dependencies in your code, but maven won't. In such a such change the dependency scope from runtime to compile.
you need to add the maven-plugin into (each) child module (for compiling main and test source)
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugins>
and then you add the plugin-management into the parent pom, for centralizing the plugin config (version...)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</pluginManagement>
Then you can add your dependency into the dependent module pom
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
http://www.jouvinio.net/wiki/index.php/Projet_Maven_multi-modules

Categories