In new Maven projects in IntelliJ, I see 2 ways of declaring new dependencies:
Manually editing the pom.xml file, adding a <dependency/> declaration.
This automatically (maven auto-import is on) adds Maven: ... entries to the project .iml file and all is well.
Using the File -> Project Structure -> Dependencies menu. This only creates entries in the .iml file.
This seems like unwanted behavior. IntelliJ doesn't overwrite the .iml file when importing the pom.xml Maven file, but merges dependencies from the pom.xml and additionally-defined ones from the .iml.
I want to enforce a single way of adding dependencies in my team (using pom.xml only, for external mvn builds), and the ability to bypass the pom.xml and directly add dependencies to the .iml creates the illusion of a valid build (when in fact, it is not).
Am I missing something? How can I enforce one way of adding dependencies? Is there a way through which IntelliJ will add dependencies to the pom.xml file instead of the .iml files?
EDIT
In this question (IntelliJ IDEA + Maven what is the need for dependency entries in an iml file?) it is claimed that
This can be also used to experiment with dependencies without changing the pom.xml. Note that all the modifications you make will be reverted on next Maven import.
But this is not the behavior I see. The .iml file doesn't get reverted, but merged.
It sounds like you need a continuous integration environment.
This won't stop your team members adding dependencies in the wrong place, but it will ensure that is caught very early and flag it up that they need to add it to your pom, then it just comes down to education.
Pom.xml is only to be used by Maven, IntelliJ does pick up the dependencies from the pom.xml if you have configured your IDE properly. If you however, want to use maven solely, you can add the jars to your local maven repo and then call them from your pom.xml normally via the <dependency> tag.
You can learn how to add local jars here.
Related
I added com.googlecode.libphonenumber to my POM file. I expected to be able to add it as required to the module-info file, but I couldn't. I can see in the .m2 directory. I can see it in the IntelliJ project .iml file.
I can see in the external dependency list along with all the other dependencies. Nothing seems wrong, yet I am not able to use the library in the project and IntelliJ suggests I import maven dependency (nothing happens, that was step 1). I tried to invalidate cache and restart. I tried deleting .idea and .iml file and re-importing the project. This is one of those times when I have nowhere to go but here. It just doesn't make sense.
requires libphonenumber; in module-info did the trick. IntelliJ was not suggesting this, I had to do it manually. Nor did it recommend it in a drop down list of possible dependencies. Java modules still need some refining.
I have a JAR. It is not a FAT JAR. It only contains my classes. But my JAR has a dependency upon azure-servicebus. I don't want to add azure-servicebus to my jar and make it a fat jar.
I just want that when the project adds my jar, it should download azure-servicebus automatically.
I am using Maven to create a jar (without dependencies).
How do I specify that? Is this possible?
edit:
I want the project that is adding my jar as a dependency should download azure-servicebus WITHOUT project having to add dependency for azure-servicebus or me packaging azure-servicebus within the jar file.
Assuming you already have a maven structue in your project, you would add the following line to your pom.xml:
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>1.2.12</version>
</dependency>
If you don't have a maven structure, you'd need to generate one. This usually can be achieved within the IDE you're using.
If you want the resulting jar not having the service-bus, you can make the dependency some kind of compile only, which would be more or less a duplicate of this question: Is there a Maven "compiler-only" scope for dependency artifacts .
However, this would imply that your target runtime has to include the definition for the classes in some way (via -cp switch maybe).
From my POV: If you need a dependency to run your program, include it as long as the licence allows that.
If I understand you correctly, the structure you are talking about is:
some-project depends on your-jar which depends on azure-servicebus.
If some-project declares a Maven dependency on your-jar, then it gets azure-servicebus automatically as dependency because Maven does transitive dependency resolution. So when you build some-project, azure-servicebus will be on the class-path and if some-project is a WAR/EAR, than azure-servicebus will be part of that WAR/EAR.
I asked this question without knowing something very important.
When jars are put into artifactory, a corresponding .pom file also has to be placed alongside it (outside of the directory). This pom file is what tells the dependent project that the jar you are dependent upon, requires so and so dependencies itself.
This answer helped me understand:
https://stackoverflow.com/a/50002072/4828463
Thanks to everyone who tried.
I have a project that generates some classes and resources that should then be packaged into a jar.
The class that does the generating has several dependencies. The generated classes have no dependencies.
My initial solution was to use the maven assembly plugin to only include the generated files. Unfortunately, the packaged pom includes all the dependencies required to do the generation. Consumers of this jar pull in a bunch of unnecessary dependencies.
I looked into the maven shade plugin. I compile once, run the generator class with mojo's exec plugin, the compile a final time. Then shade of course runs in the package phase. It creates a dependency-reduced-pom.xml without the excessive dependencies. So run mvn clean package and look in target/foo.jar. I expect the jar in the meta-inf folder to be the reduced dependency jar. But it's the full pom. I don't know how have the generated pom replace the one that is packaged.
Another poor solution I tried was using multiple profiles with their own dependency section. I run maven once with the generating profile, then again with the packaging profile. This works, but sucks because you have to run multiple maven commands. It's not jenkins friendly.
Am I going about this the wrong way? How should I go about arranging a project that generates some code and some text files that are then packaged in a maven friendly artifact?
1) One possibility is to make all your dependencies <optional>true</optional>.
The pom will still have the dependencies but the projects using your library won't try to download them.
2) Use plugin dependencies with the exec plugin.
That works well if the generator class is defined on another project.
Ideally you would call your generator using a plugin and avoid adding those dependencies to your project. That may not be worth doing depending on what your project is for and how reusable your generator has to be.
3) If you are using maven-shade-plugin the dependency-reduced-pom.xml is normally used. Check your .m2 repository to see the contents of the pom that is copied there when you do mvn install. It's likely that its contents will match the dependency-reduced-pom.xml
Is it possible for maven plugin to manage only dependencies and nothing more.
I work with "strange" maven project, and want Eclipse/maven plugin only to read dependencies from pom.xml and add it to project classpath. And nothing more.
I don't want it to set exclusion filters, source folders and output folders, or to overwrite other dependencies.
Also, pom.xml is not located in the source folder of Eclipse project. I know I could use mvn eclipse:eclipse task manually, but it mess with my .classpath and .project files, which I don't want to merge manually.
To summary, I want that all dependencies from pom.xml are automatically managed by plugin, but for plugin not to touch anything else.
EDIT: The problem is that whenever something in pom.xml changes, maven plugin changes my project configuration.
EDIT: It has to be maven since there is already pom.xml which I can replace with sbt or ivy or lein or anything eles.
Does it have to be maven? If you don't need any of the plugins, or project organisation you could use apache ivy instead.
Or you could use a even more simple one like SBT
if you have to use maven, just strip out the plugins from the pom.xml file and only add the dependencies and repos and use an IDE to launch the aplication or create a jar.
you will need to run mvn commands thought if you change the dependencies.
I've been trying to add a custom .jar (ftp://ftp.ncbi.nlm.nih.gov/pub/eutils/soap/v2.0/java/axis2_1.5.2_jdk_6.0.12/eutils_axis2.jar) to a project that doesn't have a central corporate maven repository and that instead will have the custom JARs checked into SCM in the project directory tree. I was following this post to make it happen: Maven: add a dependency to a jar by relative path (awesome post btw).
What I did was:
Add local repository to pom.xml
install the file into the local repository
Add dependency to pom.xml
Based on what I see in m2eclipse, the library has been successfully recognized by Maven and added to the dependency list (or it'd be called ? : ? or something similar)
The problem is that Eclipse still doesn't see the referenced lib, so this still fails:
import gov.nih.nlm.ncbi.www.soap.eutils.*;
Pardon my maven newbiness, but what are changes / next steps I need to make to get to:
Have Eclipse see the library so that autocomplete works (and the import can be resolved)
Be able to compile the project
Be able to execute the jar produced by mvn package?
Thanks!
If you see the JAR under "Maven Dependencies" in your project, Eclipse should be able to see and use it. If it's not there, then m2eclipse wasn't able to resolve the dependency properly.
If it is missing, m2eclipse was unable to download the dependency from your local repository for some reason. Check the Maven 2 Console for errors and the Problem View.
Lastly, the JAR itself might be corrupt. Maven doesn't check for that, it simply adds the file to the classpath. If Eclipse can't open the JAR, you can also get the errors you mentioned.