I have a class in an application (it is a Spring Boot project), for example:
package com.exemple.spring.springboot.model;
public class Employee {
...
}
And in the pom.xml I have this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exemple.spring</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
...
Now I want to use this class Employee in another application like this:
package com.exe.spring.controller;
import org.springframework.stereotype.Controller;
import com.exemple.spring.springboot.model.Employee;
#Controller
public class EmployeeController {
public Employee getAnEmployee() {
...
}
}
I've build the JAR with mvn clean install for the first app and I've added it in the pom.xml of the second app like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.exe.spring</groupId>
<artifactId>rest-client</artifactId>
<version>1.0-SNAPSHOT</version>
<name>rest-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.exemple.spring</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
And the problem is that I cannot import the Employee class in the EmployeeController class. I've imported it manually like this import com.exemple.spring.springboot.model.Employee;, but Intellij show me an error: Cannot resolve symbol 'springboot'. I see that the dependency is presented in the local repository. It is very weird because Eclipse doens't see anything to import for Employee, and Intellij see the class but when I try to click on "Import class" it doesn't import anything. What can I do to import it correctly? Thank you!
You can not directly add it as dependency, it has to be present in one of certral/ local / remote repository.
You need to add the jar to repo, e.g. local repoository,
mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
And then add repository as,
<repository>
<id>repo</id>
<url>file://${project.basedir}/repo</url>
</repository>
Check other optios here
Check follow:
You've used mvn clean install to build and install the first JAR.
You have your JAR in the classpath of the second app. You can see all
of them in "External libraries" in idea. If not, reimport maven project or use auto-import option
Also you can use different maven project structure e.g :
Common parent
|
|-------> first jar
|
|-------> second jar (with first one as a dependency)
I think it should work better for you.
Related
The problem I have is that I have a Library which I want to use in the Main App. In the Library I have added external libraries, and the question is, is it possible to use directly code of this external libraries in Main App despite of that these libraries are not added directly to Main App but to my Library?
I've added my Library to pom.xml in Main App and I can use the code that is written there, but I have problem using Library classes that extends external library classes, because Spring throws ClassNotFoundException
I've tried #ComponentScan in Main App and as argument I passed the main package of Library (com.project.common.starter), but it helped only with Components of the Library, but no external ones
---EDIT:
my Library pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/>
</parent>
<groupId>com.project.starter</groupId>
<artifactId>project-commons-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project-commons-starter</name>
<description>project-commons-starter</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.external.library</groupId>
<artifactId>external-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
my Main App pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/>
</parent>
<groupId>com.project</groupId>
<artifactId>project-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>main-app-name</name>
<description>main-app-desc</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
...
<dependency>
<groupId>com.project.starter</groupId>
<artifactId>project-commons-starter</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
...
</project>
Class in Library that probably causes problem:
package com.project.starter.service;
import com.external.ExternalLibraryProcessor;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
#Component
public class LibraryProcessor implements ExternalLibraryProcessor {
#Override
public void process() {
String context = MDC.get(ContextUtils.REQUEST_CONTEXT)
doSmth...
}
}
my MainPortalApplication :
package com.project
#SpringBootApplication
#ComponentScan(basePackages = {"com.project","com.project.starter"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
ContextUtils.class:
public class ContextUtils {
private ContextUtils() {
}
public static final String REQUEST_CONTEXT = "requestContext";
}
When I run application I get:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to read candidate component class: URL [jar:file:/home/***/.m2/repository/com/project/project-commons-starter/0.0.1/project-commons-starter-0.0.1.jar!/com/project/starter/commons/ContextUtils.class];
nested exception is
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.project.starter.service.LibraryProcessor] for bean with name 'libraryProcessor' defined in URL [jar:file:/home/***/.m2/repository/com/project/project-commons-starter/0.0.1/project-commons-starter-0.0.1.jar!/com/project/starter/service/LibraryProcessor.class]: problem with class file or dependent class;
nested exception is java.lang.NoClassDefFoundError: com/external/library/ExternalLibraryProcessor
The issue in my case was that I was using wrong maven command to compile my Library:
mvn install:install-file [...]
When using above .pom file has no dependencies which where included in Library.
When changed to mvn install it started working well - .pom file started to look like it should and Main App recognized transient dependencies.
When I want to start a new maven project in STS and make my first simple spring project from tutorial JavaBrains I received this error:
Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:maven-clean-plugin:maven-plugin:2.6.1:runtime Cause: error in opening zip file
I dont know what happen...
my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.test.spring</groupId>
<artifactId>course-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Test Spring API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version> // i try another version and this same..
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
Remove the contents from the below folder can help fixing this issue. Hope this helps.
C:\Users\.m2\repository\org\apache\maven\plugins\maven-jar-plugin\
Then run: mvn clean install
You need to remove the existing versions from your local maven repository.
Then run a clean install with -U option.
it seems issue with your pom.xml file,
Make sure <artifactId>spring-boot-starter-parent</artifactId> is available in the repository, looks like you have edited artifact name as <spring-boot-starter-**parent**>.
Your second console says that you have commented single line using <//> in your pom.xmlso it's printing as expected START_TAG or END_TAG not TEXT (position: TEXT seen ...</version>\r\n\t</parent>\r\n\ use <!-- --> to comment in pom.xml file.
Hope this helps to resolve.
I am learning Spring in maven environment. I am using netbeans.
pom.xml
<dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.davidsalter.cookbook</groupId>
<artifactId>springMavenTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
</project>
And spring.context-4.3.4.RELEASE.jar appears under Dependencies. Under Java Dependencies i only have JDK1.8.
When i have to configure my beans in a java class using #Configuration and #Bean annotations i can't import org.springframework.
It seems that you just create a maven project in NetBeans. The maven dependencies you declared in pom.xml will be downloaded and listed in the Dependencies folder.
Right click on Dependencies folder and click on Download Declared Dependencies to reload the dependency. If still failed, you may need to check your maven proxy in setting.xml.
Update you project.
By doing Alt + F5 and then select your project then click Ok
Else right click on your project -> Maven -> Update Project.
Make sure you are connected to the internet to download all these spring dependencies.
I had the same problems. I added the following to the pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
It seems to work ....
I had a multi module application called ParentApp(Which is a parent project) and it has JARMOdule, WARModule, EARModule. EAR Module has dependedncies of JARModule and WARMOdule
ParentPOM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ParentApp</groupId>
<artifactId>ParentApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>JARModule</module>
<module>WARModule</module>
<module>EARModule</module>
</modules>
</project>
EARModule POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ParentApp</groupId>
<artifactId>ParentApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>EARModule</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>ParentAPP</groupId>
<artifactId>WARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>ParentAPP</groupId>
<artifactId>JARModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
And respective JAR and WAR Poms which doesn't have any dependecies, WARModule is a WebApp, I had a class in WARModule when i build the ParentApp with mvn clean install
under target folder under classes i still have
I'm basically trying to have a rest service in WARModule but unable to make it work, any help is appreciated. Thanks!!
You need to follow the Maven standard folder structure.
You should put all your java code inside src/main/java folder.
Looking at the image you uploaded, it looks like the SampleApp.java is not present in the correct directory as expected by maven.
I have set-up a modular project in Maven with a parent project having more than one child projects.
The pom.xml of parent looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
<packaging>pom</packaging>
<dependencies>...</dependencies>
<properties>
<parent-application.version>1.0</parent-application.version>
</properties>
<modules>
<module>parent-model</module>
<module>parent-masters</module>
<module>parent-web</module>
</modules>
</project>
The pom.xml of child projects looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
</parent>
<packaging>jar</packaging>
<artifactId>child-model</artifactId>
<dependencies>...</dependencies>
</project>
Now, I need to use one of the child projects as a lib in a separate unrelated project. The pom of the new project looks like below.
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>unrelated-app</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>child-model</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I keep getting the below error
Illegal character in path at index 57: http://repo.maven.apache.org/maven2/com/company/parent-app/${parent-application.version}/parent-app-${parent-application.version}.pom
The reason seems that I am inheriting the version attribute in child-model from prent-app.
Is there a way to overcome this issue? OR do I need to provide the version for each child module in respective pom.xml and cannot inherit from common parent.
Thanks in advance.
There is a way to overcome this issue with relativePath but I would not recommend it (take a look at this answer)... You should always provide version for parent module. To update all versions of all modules (parent+children) you can use maven-version-plugin or maven-release-plugin.