How to add maven modules to IntelliJ? - maven

IntelliJ cannot find the classes in a Maven module, even if I add the jar dependency into pom.xml.
I created a Maven module: test-intellij . And then I created a class TestApp and made it implements ApplicationContextAware as below:
public class TestApp implements ApplicationContextAware{
}
IntelliJ told me: "can not find class ApplicationContextAware".
So I pressed "alt + enter", then from the popup tips I chose "Add maven dependency".
After this operation, the dependency below was added into pom.xml successfully:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
But when I try to import the ApplicationContextAware class , IntelliJ still cannot find the ApplicationContextAware class to import.
Could anybody help me to solve this issue?

Try to Reimport the Maven project using the corresponding button in IntelliJ IDEA Maven Projects tool window. Wait until the dependency is downloaded and indexing is complete. File | Invalidate Caches may also help.
Also check that you are using the latest IDEA version (12.0.2 at the moment). You should be able to browse inside the downloaded dependency jar in the Project View, External Libraries node.

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 ?

Unable to resolve #SpringBootTest with a maven dependency with scope test

I added this repository to pom.xml, but IntelliJ fails to resolve org.springframework.boot.test:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
However, after adding the library to classpath, it happens to work:
What's so special about this repository that I should add it manually?
EDIT:
<scope></scope> was causing this problem. I had no idea of what it actually does, and it made IntelliJ ignore my main directory.
Deleted it manually from the local maven repository. If it still not working. Add the older version to pom.xml.
Hope it work
After adding the dependency to the pom.xml do a MAVEN REIMPORT. If it doesn't solve, go to main menu and do invalidate cache/restart ide. Then you are good to go.
Problems :
I was also facing the same issue but with the Gradle.
The #SpringBootTest and all JUnit dependency like #Test were not resolving.
In my build.gradle, I had dependency like this
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Solution
The problem is that the test dependency is not visible to the folder in which your test class is present. You need to have it in the same structure as shown below.
More things you can do
Then telling IntelliJ about my test folder as in the image below so that I can directly use the shortcut to create the test class at the right location.
After that to create a test class for a particular class, on a mac I use CMD+Shift+T and then select the test folder.
I also had the same issue wiht my project. I tried Maven->update project(Alt-F5). This will update the maven dependencies once and builds your project.
It helped me resolving the issue.

Dependency listed in pom file not found in deployed project

I asked a question here that I think I may have found the root of. I have a Spring Boot app using a datasource, net.sourceforge.jtds.jdbc.Driver, that is supposed to be included transitively by Spring Boot 2.0.2 with spring-boot-starter-jpa. However, when I run
jar tf my.jar | grep jtds
the driver class isn't found (we don't have a maven executable on the server to list the classpath). Everything I do to inspect the classpath reflects that the jar isn't there.
I've done this in 2 scenarios: 1) When I didn't explicitly add the jar to my pom, I got the error reported in my previous post. 2) When I do add it explicitly to the pom, I get this error:
java.lang.IllegalStateException: Cannot load driver class: net.sourceforge.jtds.jdbc.Driver
Can someone tell me what's going on?? I am confounded as to why this class can't be found and loaded.
Please mind, that in the Spring Boot Parent POM the jtds dependency is only included in test scope.
If you want to use classes of this dependency also in your production code, please change the Maven scope to compile.
Ok, the problem was solved by adding the dependency with a runtime scope.
In child pom where jar is packaged, you should have
spring-boot-maven-plugin. and dependency as below:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
</dependency>
In parent pom :
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>${jtds.version}</version>
</dependency>

Maven: The type cannot be resolved. It is indirectly referenced from required .class files

I changed some existing projects from ant to maven projects.
So far so good.
All projects do have the same groupId.
Theres a project with name "ServerBase" and artifactId "server-base".
Within this project theres an abstract class "BaseService" which defines a logger via:
import org.jboss.logging.Logger;
[...]
protected Logger log = Logger.getLogger(this.getClass());
Theres another project with name "Server" and artifactId "server".
Within this project theres a class ConfigurationDAOImpl extending the BaseService-Class above.
Within ConfigurationDAOImpl the logger log is used for creating some outputs.
Within the "Server"'s POM file I have declared:
<dependency>
<groupId>com.tcom.amadeus</groupId>
<artifactId>server-base</artifactId>
<version>${project.version}</version>
</dependency>
Under BuildPath the dependency is shown very nice under MavenDependencies. I removed the old dirct/natural/ant-dependency from build path before.
If I remove it I am getting very much errors about missing classes etc.
But although I do have this dependency I am getting the followin error in eclipse (under tab markers):
The type org.apache.commons.logging.Log cannot be resolved. It is indirectly referenced from required .class files
Resource: ConfigurationDAPImpl.java
Path: /Server/src/main/...
Location: Line 24
Type: Java Problem
I tried removing the dependency and add it again but without any luck.
Both projects do refer to JAVA 1.8.
Both projects have been build with targets clean an package multiple times.
Both projects have been updated by Righclick or pressing F5.
I am using Eclipse Version: Neon.1a Release (4.6.1)
I am using apache-maven-3.3.9
I am using m2e Plugin.
Any further help would be grateful.
Thanks in advance.
There are two ways to 'solve' this:
1)
explicitly add the required dependency within the server-projects pom-file:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
2)
change the scop of the required dependency within the server-base-projects pom file from up to now 'provide' to 'compile' or erase the scope tag at all such that the default scope is used by maven (which I guess is 'compile')
old:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>provided</scope>
</dependency>
new:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope></scope>
</dependency>
or:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
Some background to this from documentation:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies
provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example,
when building a web application for the Java Enterprise Edition, you
would set the dependency on the Servlet API and related Java EE APIs
to scope provided because the web container provides those classes.
This scope is only available on the compilation and test classpath,
and is not transitive.
Thanks all.
It looks like apache logging library is not brought transitively from your server-base project. Check if in project server under MavenDependencies you see commons-logging (apache logging) jar. If not, then add this as your maven dependency in server-base project.
Repeat the above for all jars that server-base depends on.

AEM 6.1 Maven Dependency Resolution

I'm using AEM6.1 with Maven as build manager.
I want to extend the com.day.cq.dam.core.process.ThumbnailProcess class.
Using the AEM buit-in dependency resolver
system/console/depfinder
it seems that the class should be contained in
<dependency>
<groupId>com.day.cq.dam</groupId>
<artifactId>cq-dam-core</artifactId>
<version>5.8.172</version>
<scope>provided</scope>
</dependency>
Anyway the Adobe nexus repo does not contain this version and all the older do not provide the above class.
I wish to know how to manage this problem.
Thanks for any advice.
In the latest version Adobe pushed everything into a giant dependencies jar.
Put this at the end of your POM:
<depen­dency>
<groupId>com.adobe.aem</groupId>
<arti­fac­tId>uber-jar</arti­fac­tId>
<ver­sion>6.1.0</ver­sion>
<scope>pro­vided</scope>
<clas­si­fier>obfuscated-apis</clas­si­fier>
</depen­dency>

Resources