Intellij Spring maven install error "package does not exist" - spring

Intellij version : Community 2019.2.3
Maven : 3.6.2
Spring : 2.2.0
I am trying to create a very simple Spring maven project with two sub-modules (one independent and another one dependent on independent one).
Root module - testmultimodule
Independent module - independent
Dependent module - dependent
testmultimodule pom.xml has all Spring related declaration and module definition
<modules>
<module>independent</module>
<module>dependant</module>
</modules>
Independent poom.xml is simplest and . only has parent maven declaration
<parent>
<artifactId>testmultimodule</artifactId>
<groupId>in.org.app</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
dependent module pom.xml has the dependency declaration as below to independent module
<dependencies>
<dependency>
<groupId>in.org.app</groupId>
<artifactId>independent</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
I have created a Test class under dependent module and using a User object from independent module. Initially, without the above dependency declaration, asa usual there was compilcation error.
As soon as I add the dependency and builld the project within Intellij IDE with the option "Build Prooject" option from "Build" menu, it successfully builds.
However, if I try to use Maven install option within Intellij right side window option. It always fails stating Error:(3,33) java: package in.org.app.independent.bo does not exist .
I am providing the GitHub URL for the test project , if you want to take a look and test by yourself.
GIT URL:
https://github.com/DhruboB/testmultimodule
I have tried all sort of tweaking found in internet so far e.g.
clearing Intellij Cache & restarting, mvn -U clean install, mvn scope verification, proxy etc.
Any further idea to resolve this? I need to solve this in the Community version of Intellij.

Your parent project includes the definition for the spring-boot-maven-plugin. This leads to each project defining this as a parent to be repacked to an executable JAR by this plugin. This repackaged JAR isn't useable as a dependency in another project.
Either you need to change the configuration of the spring-boot-maven-plugin for the project you want to use as a dependency. This is explained here in the Spring Boot Reference Guide. You now basically have 2 jars from this project, one plain and one executable.
If you don't need that project to be an executable JAR file then just move the spring-boot-maven-plugin to the project that needs to be. All other projects will no be basic JAR files again.
See also How to add a dependency to a Spring Boot Jar in another project?

Related

Cannot access class and methods from custom jars in Maven

I have created a project in java and compiled it as a jar with the mvn clean install command. The jar is in my local .m2 folder. I am trying to import it as a dependency in a new project. The jar is successfully imported into my new project but I cannot access any classes or methods in that jar.
<dependency>
<groupId>com.test</groupId>
<artifactId>rest-methods</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
This my custom maven dependency imported in my External Libraries
And this is when I try to use these class and any method inside it in my project:
When I try to add the maven dependency manually I can find it and I can click add but nothing happens
Am I missing some steps for using this class in my project? I am working on a Ubuntu 20.04 operating system
UPDATE: After looking in the Project Structure -> Project Settings -> Libraries I see that several files from the jar are marked as excluded (sources and JavaDocs):
Did they not get build properly when I created the jar ?

Custom pom.xml filename in Maven multimodule for Tycho

I have a project with a couple of dozen Eclipse plugins, all inter-related and living in different subfolders. The build was converted to a multi-module manifest-first Tycho build a couple of years ago and it works quite well.
One of the plugins is rather key, and can also be built as a standalone Java app, which doesn't use an Eclipse runtime. Currently it has its own POM file (pom-standalone.xml) so that Jenkins can build the standalone app separately and the Tycho build knows nothing about it - the pom-standalone just lists the previously-built plugin jars (thanks Tycho!) and Eclipse libraries that it needs as dependencies. Couple problems with this approach though:
I cannot easily use IntelliJ to work on the standalone project with Maven dependency management, because it doesn't recognize the custom pom-standalone.xml filename as a POM.
The many jars that this project relies on are checked in to the project for the sake of Tycho and the Eclipse Manifest file, but they're also managed by Maven for the standalone build. So any dependencies have to be added to the pom-standalone.xml file AND entered into the OSGi manifest AND checked in to the source control for Eclipse purposes.
It seems like a straightforward workaround would be to tell Tycho/modules to use something other than pom.xml for the submodule's POM, or perhaps all the multimodule POM files, since Eclipse doesn't use those anyway - then the pom-standalone.xml can be converted to pom.xml and then IntelliJ will be fine with it.
I know you can specify the -f attribute to Maven builds, but will that really apply to all submodules? Can you specify the POM filename for just ONE submodule?
Are there alternative solutions? Eclipse/Tycho/p2 builds seem somewhat of a headache requiring manual library management and checking in libraries to source control, but maybe there have been changes I'm not aware of in the Eclipse build world the last few years.
Found a Similar Question that didn't help much.
You can include projects in an aggregator POM by specifying the full name to the POM file with custom name. Example:
<modules>
<module>org.example.bundle1</module>
<module>org.example.bundle2</module>
<module>org.example.keybundle/pom-tycho.xml</module>
</modules>
This both works in a pure Maven and Maven/Tycho build.
To extend #oberlies answer a little bit:
SCENARIO: top aggregation POM comes in multiple flavors, so any style can be built from the top.
<!-- in file pom.xml -->
<modules>
<module>org.example.bundle1</module>
<module>org.example.bundle2</module>
<module>org.example.keybundle</module>
</modules>
All submodules will be built using their standard pom.xml
and
<!-- in file pom-tycho.xml -->
<modules>
<module>org.example.bundle1/pom.xml</module>
<module>org.example.bundle2/pom.xml</module>
<module>org.example.keybundle/pom-tycho.xml</module>
</modules>
Submodules will be built using the specifically named POM file.
and, likewise:
<!-- in file pom-special.xml -->
<modules>
<module>org.example.bundle1/pom.xml</module>
<module>org.example.bundle2/pom-special.xml</module>
<module>org.example.keybundle/pom-tycho.xml</module>
</modules>
Submodules that have custom POM files use them, and others state they still want the normal POM file, all independent of the name of the top aggregation POM file.
Because mvn -f pom-tycho.xml assumes that file name in all submodules. So if you do want pom.xml in any submodule when the top file isn't named pom.xml you need to fully specify for each submodule.

How to manage maven depend project from Artifactory?

Inside a Parent project i have multiple project and take this simple example Project common-3rdparty used as a depend project for Project B and in Project B pom.xml file i have added below lines
<dependencies>
<dependency>
<groupId>com.pre.cdd.common</groupId>
<artifactId>common-3rdparty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
Now i am using Artifactory for dependency management and added common-3rdparty project into Artifactory but now when i am running mvn eclipse:eclipse project build successful but common-3rdparty.jar not downloaded so Project B giving exception
common-3rdparty.jar missing
So it mean somehow common-3rdparty.jar not downloaded from Artifactory . What i am doing wrong here?
EDIT:-
Now my simple question is that if i have 10 project and 9 project needed to run 10th one so we added 9 dependencies in 10th project pom.xml now what changes i have to do in Artifactory side or pom file so it will easily get depended projects.
The remote-repos contain only the remote repositories (hence the name). If you deployed the jar into Artifactory yourself, it resides in one of the local repositories, that's why maven can't find it. You should use lib-releses instead of remote-repos in your maven configuration.

Add a module dependency in Maven

I have got two Maven projects. One of them has to be dependent on the other. I use IntelliJ and I have tried to right click on project1 > Open Module Settings, and in the dependencies tab I clicked on the + symbol to add a directory or jar dependency. So far so good, when I try to import packages from the dependency it autocompletes it for me, however the compilation throws errors, saying that there are no such packages. What am I doing wrong ?
There is no notion of project in Maven.
You have a Maven project B. You chose its groupId (com.mycompany, for example), its artifactId (B, for example), and its version (1.0-SNAPSHOT, for example). You run mvn install on this project. This generates a B-1.0-SNAPSHOT.jar file and stores it in your local Maven repository, with its pom.
Now you want to use B-1.0-SNAPSHOT.jar in another Maven project A. For A, B is a library, just like any other library you use (log4J, Spring, Hibernate, Guava, whatever). So you add a dependency to it in the pom of A, just like you do for any other library:
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- other dependencies: log4J, Spring, Hibernate, Guava, whatever -->
</dependencies>
Read the awful documentation for more details.

about generate maven dependency

I am pretty new to maven.
Now I have a maven project developed. My another project needs to depend on this one.
Does anyone know how can I generate my own dependency? So that my second project can add the first one as a dependency in pom.
thank you very much
Since your first project is already a maven-project, just install it in your local repository by running mvn install in the first project's root directory.
Then you can include a dependency in your second project by simply referencing the groupId, artifactId and version you defined in the first project.
So if your first project had the following in its pom:
<project>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
... <!-- more here -->
you can include this in your second project:
<dependencies>
<dependency>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Unless you deploy your project 1 jar to a central maven repository, this will only work if your jar is in your local repository (via mvn install).
Maven projects are identified by the "Maven coordinates", that is, the ArtifactID, GroupID and version.
Say you create your first project and run maven install. Your local repository (in $HOME/.m2/) will now contain the compiled project plus whatever coordinates you put in there.
Your second project must now only depend on the said coordinates.
I would suggest googling a bit on maven. I made a tutorial a long time ago that might help you, even if the examples are a little simple. Here you go and good luck!

Resources