Use classpath of multiple modules in IntelliJ IDEA - java

I have a Maven project, imported from Eclipse, where the dependencies are set to scope provided. When the project is deployed, the jars are deployed as well so that works fine.
While developing, however, I use a "debugging project" that calls the Maven project, and when it runs I get a bunch of Class Not Found errors when the Maven dependencies are set to provided.
If I change the scope of the Maven dependencies to Compile then the project works fine.
If I change the scope of the dependencies to compile, would that change the output of the project? i.e. add a bunch of jars? That would be undesirable.
I also tried to change the Debug Configuration settings and specified the Maven project in "Use classpath of module", but then the files of the debugging project are not found.
How can I specify the classpath to be of both the Maven project and the debugging project, so that classes from both projects including the dependencies will be on the classpath?
Thanks!

There are 3 types of dependency scope: compile, test, and provided,
compile: the dependency library will be used in all steps: compile , test and run,
test: the dependency library will only be used in the test
provided: the dependency library will only be used in compile and test, but in the run time, the dependency library must be provided by the container otherwise it will throw class no find issues.
Your issues is that you did not provide the dependency library in the run environment ( container) when running your project.
hope this can help you

How did you import the project to Idea? If the project is opened as a Maven projects, it should work out of box.
Can you try to open the project by selecting pom.xml?

Related

Resolving Maven Dependency in Eclipse

I'm trying to resolve a maven dependency in my eclipse project.
The dependency has already been downloaded into my maven dependencies folder (as can be seen below)
However, the import statement still isn't resolving.
In my java build path, the Maven Dependencies folder is present.
So far, I've tried rebuilding the project on the command line and refreshing it in eclipse; I've tried rebuilding it in in eclipse itself; and I've tried updating the project in eclipse.
Nothing seems to work. Any help would be appreciated.
EDIT: Here's my POM file for those of you who asked.
Your dependency is set as under the test scope. This means that it's only available in test classes (e.g. src/test/java). If you want to use it in your main app src/main/java remove the test scope.
I don't use eclipse but the general idea is to delete all dependencies in your dependency folder and rebuild or you can also change the version of the dependency that is throwing the error. Sometimes, the name of the class you are calling may be changed in the version you are using.
With a right-mouse click on the project node in the Package Explorer and selecting menu Maven -> Update Project..., you can update the Maven dependencies.
You also have to keep in mind, that Maven has often dependencies in a "test" scope in the pom.xml. In that case you can use these dependencies only under the src/test/java path.

Maven Jar dependency is not coming automatically

I am quite new to Maven.
I have a Maven Project.
When I create execute using mvn clean -e install it creates executable jar (commounutil.jar) for my project in target folder.
Not I have another project (project2) in Eclipse which is not a Maven based project.
Project2 uses features and classes from commounutil.jar.
If I manually add my executable jar commounutil.jar in eclipse using Java build path/add external jar.
But even after adding this jar there are some errors in my project which are related to log4j.
Now when I build commounutil the dependency for log4j was already added.
Still it is giving compile time error.
Could you please tell me where I am going wrong?
Add log4j in your Eclipse classpath. Commonutils.jar,if it follows modularity promoted by maven,provides only common utils class and do not provide class related to its dependencies.
Maybe you should consider to update your second project to a maven project.

Not able to get directory structure and Maven dependencies

I have checked out a Maven project from Subversion. It has Java with Spring framework and Groovy.
We are using Groovy Eclipse compiler plugin. On Maven install, code compiles perfectly with Groovy Eclipse compiler and a jar is created in target.
But there is no directory structure which is usually created in Maven. No classpath too.
I tried many things nothing worked. Finally I converted project to faceted form and then src/java was created as it was faceted to Java project. Still no Maven dependencies.
As there was no Maven dependencies, many jars were missing, so I added jars to build path manually. I also added src/groovy to build path and then it was not able to compile Groovy classes, so I configured project as Groovy project and DDSL was generated. Now after so many manual settings, it is not working fine when it has to create object using spring for Maven class.
I tried a project without Spring framework, and it worked fine.
For pom.xml: I have referred: http://docs.groovy-lang.org/latest/html/documentation/tools-groovyeclipse.html.

include eclipse library in maven build

simple scenario:
I have a maven project, containing some maven dependencies (activiti framework) and added the Widlfly 8.1.0 runtime as library in eclipse.
naturally now, if i clean and build with MAVEN, maven won't consider the runtime while compiling and complain that it cannot find eg. the #Webservlet Annotation, HttpRequest classes etc.
so in order to build my Project, i have to run any maven goal and see it fail, just to have maven download all dependencies, then build the project with eclipses' build process which then uses all downloaded maven dependencies AND the wildfly 8.1.0 runtime, succeeding in building the project.
THEN only can I run maven install/deploy to create the .war, which works, because maven finds a compiled target folder, created by eclipse.
How can i, without instlalling all runtime jars to my local repository or adding the wildfly installation as antoher local repo, tell maven or the m2e plugin to include manually added libraries to mavens compile step?
what you probably want is a "provided"*-scope dependency on org.wildfly:wildfly-spec-api:8.1.0 (which is a pom artifact containing all the apis/specifications provided to you by wildfly).
assumming you intend to deploy your app inside wildfly (as opposed to embedding wildfly in your own main() somehow...) you dont need a dependency on the wildfly container.
* - note that since wildfly-apec-api is a pom artifact (and not a jar) you need to use the import scope and not provided. see this article for a complete guide. the gist is you put an import scope dependency on the pom in dependency management, and then you can put a provided-scope dependency on any specific member api/spec that you use (say ejb3, jsf, bean validation or jpa) and the versions will be taken from the spec-api pom.

how to configure dependencies in maven pom.xml?

I am converting my simple working web application (runs from eclipse) to a maven project. This is my first maven project. I have 3 external jar dependencies to it and i added then to the pom.xml my build is failing saying that it can't find those 3 dependencies. here my pom.xml file
The build is saying that the 3 packages does not exist.
package javax.servlet does not exist
package org.apache.commons.codec.binary does not exist
package org.apache.commons.configuration does not exist
What i am missing? I am running my application from Mac and in the .m2/repository i see these 3 libraries/packages present.
You need to either remove the dependencyManagement tags that surround dependencies or add the dependencies again but nested within the project tag, i.e. at the same level as dependencyManagement.
dependencyManagement allows you to fix information about dependencies across a multi-module project - e.g. like version numbers - however you still need to provide a dependencies section alongside that so that Maven knows to include them.
If your project is not a multi-module project I would be tempted to not use a dependencyManagement section at all.
Just remove dependecyManagement tags

Categories

Resources