Intellij: How to add module compile output to web-inf/classes in stead of web-inf/lib? - maven

I'm using maven to set up a war project in IntelliJ and run in it GlassFish. The war project depends on several other modules. When I run the project in debug mode, hot deploying codechanges of the changes results in NoClassDefFound exceptions. I found out that IntelliJ tries to redeploy the module jar but GlassFish keeps a lock on it so it fails. All the classes in that module are now unavailable, causing these NoClassDefFound exceptions.
IntelliJ generates the artifacts this way: the dependent modules are all added as jar dependencies as if they were external dependencies:
Now, when I remove the jar dependencies, IntelliJ tells me it found some missing dependencies and proposes a fix to add the missing dependencies.
Fixing those dependencies will add the module compile output to the WEB-INF\classes folder.
Once deployed, IntelliJ has no problem anymore hot-deploying changed classes to GlassFish since there's no jar to keep a lock on.
Problem
Every time I make a change to any pom.xml, IntelliJ refreshes the artifacts automatically, which is fine: I definitely want to see those changes appear in the artifact. However, all modules are added as jar dependencies again.
Question
How can I make sure that IntelliJ adds the compile output of project modules to WEB-INF\classes and not to WEB-INF\lib?
I found this question but it has two problems:
There are many module dependencies so if possible I'd like to avoid specifying them all one by one in the unpack goal
IntelliJ seems to ignore this. When I add that configuration and while it works perfectly in a maven commandline build, IntelliJ still refuses to add the module compile output to the WEB-INF\classes dir
I found a bugreport that asks about the same thing but for me it's hard to believe there's no way to solve this problem. Other webapp developers using IntelliJ must have this same issue, making it difficult to hot-deploy code changes, unless I did something wrong in my pom configuration.

Related

Gradle refers jars but they are not in POM nor in the code links

We migrated our maven code to gradle. When we build, we are getting error as Could not resolve all files for configuration ':***-war:compileClasspath'.
Could not find mockito-core.jar (org.mockito:mockito-core:2.8.9).
We are not referring to this jar itself.. neither we are able to locate in our code nor in pom...
How to figure out?
how to configure gradle to compile code if jar doesn't exist and pom exists in the repository or locally ?
If you can't work out where it's coming from and you don't use it at all, then you can exclude the dependency from all the configurations:
configurations.all*.exclude group: "org.mockito", module: "mockito-core"
If the dependency isn't appearing in the dependency report, then I'm somewhat at a loss as to why Gradle is complaining about it. The only other option I can think of is that some plugin is adding it in a non-standard way.

can't find my maven artifacts

I'm working in eclipse and I have a problem that I can't import my personal java libraries.
I created the libraries and 'installed' them into my local maven repo (using mvn install). This created a subdirectory related to the 'version' name that was in the POM file from when I ran the command. Which seemed fine.
So in this directory there where the usual jar files and other stuff.
When I released this file I manually changed the name of the version in the POM. going from 0.0.1-SNAPSHOT to 0.0.1-RELEASE
This seems to have worked as I would have expected.
However I can't seem to find import the new release jar.
Using the maven repositories browser in eclipse I can see that the new artifact is in the 'local' repository.
I try to add the dependency in the following methods:
Select the main project -> Maven -> add dependency.
This adds the dependency details into the pom but with a type value detail of <type>pom.lastUpdated</type>
Select the project pom.xml file -> Maven -> add dependency.
This time the artifact for the 0.0.1-RELEASE is greyed out I can select it, but I guess nothing is actually happening.
The original 0.0.1-SNAPSHOT it selectable, and if I use this I do not have a <type> detail in the pom.
I don't understand why there is a difference in the RELEASE and SNAPSHOT artifacts, as they have both been generated in the same way, and clearly they are both visible in the browser, the contents of the directory on disk are the same. The file names and contents are identical with the exception of the word RELEASE or SNAPSHOT.
I know that I can simply add in the RELEASE jar to my build path, but this seems to be a ridiculous thing to have to do if I intend to use maven (or do I need to do this).
I don't want to use an external repo for storing my artifacts, and I'm not too keen to go to the trouble of installing nexus (or similar) on my local machine (just because I've had trouble with it in the past).
What am I missing so as I can get my maven project to see my local repository and all its artifacts.
Thanks in advance.
David
ps I've already tried things such as mvn dependency:purge-local-repository which definitely pulled in / updated all the local jar dependencies.
So I've managed to work around my problem.
As such this solution is ridiculous, and breaks all the purpose of maven.
So I found (from running maven from the cli) that there was an error in the parent of the project I was attempting to use.
The parent was missing a direct link to the scm plugin (version error).
Once I solved this problem, I then returned to the sub project, and got a lot of errors from missing stuff from the parent.
Essentially it was not 'seeing' all the log4j dependencies.
The solution (well non-solution really).
Add all the log4j dependencies to the sub project.
Edit in SCM and surefire test plugins (as it also started to fail the test code due to missing junit).
So this is great.
I have to define all my dependencies on log4j and in my sub / child project's pom.xml file.
I also need to define them all in my parent's pom.
As it then still refused to run tests in my new project (that used the above as a dependency), and refused to find log4j also. I then decided to add all of these as dependencies for my current project.
Great.
I thought the whole point of maven was I could define my dependency on log4j in my my logging library that I use (which is the dependency), and then it would 'automagically' pull in all the required from this dependency.
Clearly not.
As stated at the start. This is NOT AN ANSWER it is a crazy work around.
My logging library that I use should be able to define its own requirement on a specific log4j version (such as moving from log4j to log4j2), and then when I include this as a maven dependency any change to the required dependency should be seen automatically.
But No : I have to import the dependency on log4j in my other projects also. So now if I had updated my logging library from log4j to log4j2 I would need to go to all my project that use this library and update their pom's to ensure that I have the correct version of log4j.
Seems the whole point of maven has just been lost!
Can someone please tell me where I am going wrong!
David.

Maven can't discover workspace projects, junit, other libraries

so I have started using maven (with eclipse, and a local implementation of Sonatype) not that long ago, after getting through the initial pain I though everything was working.... apparently not !
I have my main project POM and all my child POM (I am using a 'flat' hierarchy, so all the sub projects are in the same folder as the parent POM). This seems to be working OK.
The sub project where all started and 'released' and tested before I started using Maven. I went to maven to solve my 'dependencies' problems... or rather make them more complicated in this instance.
Most of the 'Maven' stuff seems to work fine, but I can't resolve the dependencies of my 'sub projects' that have been released into my local ms/repository
Each day I start up eclipse and have the same problems, I have 'unable to resolve imports' on all my personal libraries, and all those that I use for them (such as Log4j, DDlUtils etc).
I thought the whole point of maven was that I would be able to put in an import declaration for a library, add the 'groupID' and other stuff to the child POM (or parent POM). And then have all the jars downloaded automagically when I run
mvn install
against the parent.
However each day I find I have to re-insert the build path to the libraries (internal and external) via the eclipse workspace and point them to the location of the libraries that I have downloaded as maven can't seem to find them anywhere.
Specifically with running Junit test, I can run them from the workspace (using run as junit) and they all pass fine. But if I try to run them with
mvn test
I get an message saying
java.lang;NoClassDefFoundError: org/junit/Assert
(and this is the same from within eclipse running the mvn test or from CLI)
with an error message in the output of
failed to execute goal org.apache.maven.plugins:maven.surfire-plugin:2.10:test
the POM and jar are in my M2_REPO, and I can find it when I search in sonatype, why can't maven find something that is part of its own system, exists in its repo.
but this obviously works just fine when I run via eclipse and point it to an independently downloaded junit library.
I don't know what extra info you may require, and I'm obviously missing something in either the m2e plugin, my maven install or in sonatype.
please help I'm begining to loose my hair!
and may soon be forced to return to an ant/ivy solution (but don't want to have to learn 'yet another tool' (in this case ivy).
Thanks in advance
David.
ps. i'm on a windows XP platform
edit...
can I get maven to build and release the jar and pom even though it thinks it is failing the tests (which is itn't as I'm doing them manually, and they pass just fine).
If I look in the 'effective POM' tab of my parent POM, the plugin stated above is not in there. I guess this means it is a maven config setting, but where do I add it, and why isn't it automatic when I run my first set of maven tests?
edit 2....
I just found this on the apache plugin comments page (http://maven.apache.org/plugins/maven-surefire-plugin/plugin-info.html), so I ran the code on the CLI, mvn surefire:help -Ddetail=true -Dgoal=test lots of stuff was downloaded, and now the tests run, but they fail within maven ? (remember they pass when run from eclipse using 'run as junit test)... so this is nearly a solution...
It sounds like you need make eclipse aware that the projects in question are Maven projects. You may have POMs set up for your projects, but for the m2e plugin to work you need to have either created the project as a Maven project ('New' -> 'Project...' -> 'Maven' -> 'Maven Project...'), or converted it into a Maven project by right clicking it then selecting 'Configure' -> 'Convert to Maven Project'. You will know that a project has been recognised as a Maven project because there will be a little blue 'M' in the top left of its icon, as shown in the screenshot below:
The main thing to note in the above picture is the 'Maven Dependencies' library. This is set up automatically by the m2e plugin. Whenever you add a dependency to a POM then the project's build path will be configured automatically, although sometimes you may need to force it to do so by right clicking the project and selecting 'Maven' -> 'Update Maven Configuration'. The important thing is that if everything is working then you should never have to update the build path yourself. Moveover, if you do update it yourself then any changes you make will likely get overwritten the next time you run the 'Update Maven Configuration' command.
Also worth noting from the picture is that the two dependencies 'mavenProjectTest' and 'primes' have folder icons next to them. This means that they have been picked up as workspace projects. For this to work the project must have 'workspace resolution' enabled, and the projects to be picked up need to be configured as m2e Maven projects as well.
You mention downloading external libraries. You shouldn't need to download any libraries yourself - by adding the right dependency declaration for an external library then Maven will download it from your configured remote repository (the first time anyway - afterwards it will then be able to get it from your local repository). By default, this is the Maven Central repository. To add an external library to your project just follow that link, enter the library in the search box, click on the version link for the version you require, then you will be taken to a page where there will be the dependency XML declaration that you can just copy and paste into your POM.
One more thing that may help is that you should make sure that your source folders follow the Maven default directory structure. That is, your test packages should be contained in a source folder called 'src/test/java', and the main project packages should go in 'src/main/java'. Otherwise Maven will not know where to find your source code. It is possible to configure a POM to tell Maven to expect your source code to be in different source folders, but it is highly recommended that you follow the standard Maven directory structure.
Since you have child modules, my suggestion would be to simplify things by creating a new Maven project from scratch that you can have a play around with. Once you are comfortable with that then try getting m2e to work for your multi-module project.
I remember your pain when first getting to grips with Maven, but it's great once you understand what it's doing and everything is working. I highly recommend reading through the free online book Maven: The Complete Reference - it helped me out a lot when I was getting started with Maven.
I have experienced the same messages and have found a working solution, thanks to one of my organization's Maven experts.
Here was my pom.xml that reproduces your error:
<dependencies>
<dependency>
<groupId>org.junit4</groupId>
<artifactId>org.junit4</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
With it, I would get package org.junit does not exist messages and NoClassDefFoundError: org/junit/Assert. Similar to your experience, it worked great from Eclipse, green bars and all.
Here is the pom.xml that works:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
I copied this from the example at the top of this Using JUnit page.

maven - how does it work? Missing some some jars

I am trying to move my MyEclipes projects to maven. But of course there are problems. After creating a web priject I get missing jar files - about 5
org.springframework.security jar files e.g. org.springframework.security.ldap-3.0.5.RELEASE
show as missing in the jar build path. They are not in the corresponding .m2 directory. I un-installed ME4S, and deleted .m2, which force .me to be rebuilt on re-install, but it has the same problem.
How do I fix this?
It would be very helpful to understand how the .m2 process works - where is this coming from and how is it controlled?
I am not sure about the MyEclipse part, but this seems to be a pure maven question.
Maven (2/3) uses the pom.xml. This file describe your project. In that file you should define a list of dependencies (which can have their own dependencies and so on).
Maven read the pom.xml and build the classpath accordingly using direct and transitive dependencies.
You can use the mvn dependency:tree command to see how your classpath is built.
More on the plugin page

IntelliJ: Including jars in a jar artifact

Developing on the Mac with IntelliJ 9.0.2 Community Edition.
I have a program which depends on two library jars. I have figured out how to get IntelliJ to make me a jar of my source (with the Artifact tab), and even to include the two jars in it.
However, if I get a listing of the jar file it produces, it looks like this:
com/acme/MyClass1.class
com/acme/MyClass2.class
...
mylib1.jar
myLib2.jar
And, no surprises, if I double-click the jar file, it gets a NoClassDefFoundError the first time it tries to access a class in one or other library jar.
It seems I need to tell IntelliJ to "inline" the library jars -- but this menu option is always greyed out!
Does anyone have any idea how to get jars inlined in a jar artifact?
IDEA doesn't support it yet, you can use Ant integration to package your jar (either by unpacking all the jars into the temp folder and then packaging the project output plus this temp folder into the single jar or by using some Ant task like jarjar).
If you want this feature to appear in the future IDEA versions, please vote for the request.
Having the dependency JARs included in your JAR should allow yoru code to run successfully. You probably don't have the JARs on your classpath.
I will use Maven Assembly plugin. Its simple and will give you a neat little jar file..

Resources