Compile project that references installed project - maven

I have a library module that I created through Maven, there is pom.xml what get 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.library</groupId>
<artifactId>commons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>commons</name>
<description>common dependencies library</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
With the above pom, I successfully installed it into the local maven repository by using mvn install.
In another project my-service, which uses this installed library, within the IntelliJ IDEA, it is fine compiling and running. There is the pom.xml for the my-service
<?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.example</groupId>
<artifactId>my-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-service</name>
<description>My Services</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- other dependencies ... -->
<dependency>
<groupId>com.example.library</groupId>
<artifactId>commons</artifactId>
<version>1.0.0</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
However, when running with mvn compile, I got the following errors:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-service: Compilation failure: Compilation failure:
[ERROR] /Users/doe/IdeaProjects/my-service/src/main/java/com/example/myservice/configs/WebSecurityConfig.java:[3,45] package com.example.library.commons.filters does not exist
[ERROR] /Users/doe/IdeaProjects/my-service/src/main/java/com/example/myservice/configs/WebSecurityConfig.java:[17,13] cannot find symbol
[ERROR] symbol: class SessionFilter
[ERROR] location: class com.example.myservice.configs.WebSecurityConfig
[ERROR] /Users/doe/IdeaProjects/my-service/src/main/java/com/example/myservice/configs/WebSecurityConfig.java:[19,36] cannot find symbol
[ERROR] symbol: class SessionFilter
[ERROR] location: class com.example.myservice.configs.WebSecurityConfig
The library project is created as part of the IntelliJ IDEA module within the same project as the my-service module. I guess the IntelliJ IDEA knows how to internally references module to compile the depending modules, but with Maven, it is a different story.
My goal is to compile and package the my-service module as a single jar that includes the commons library module. Now I can't even get it to compile with Maven, what did I do wrong?
Apache Maven version 3.3.9 . Operating system: OS X.

It turns out the Spring-boot-maven plugin is not needed in the library project because a library doesn't need to produce an executable ueber jar. After removing the plugin, it compiles successfully.

Related

maven multiple modules springbootr:boot run error

error message:
The POM for rc:common:jar:1.0 is missing, no dependency information available
parent pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>rest</module>
<module>common</module>
</modules>
rest pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>rest</artifactId>
<name>zeyo rest</name>
<!--<version>1.0</version>-->
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>rc</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
common pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>common</artifactId>
<name>common</name>
<version>1.0</version>
<packaging>jar</packaging>
Once you have a dependency called rc -> rest -> common.
Why is this happening?
That simply says that common project not yet built and we started rest.
First run the maven install on parent project so that 'common' project will be built as well.
spring-boot:run : does not build the dependent jars.
Hope this helps.

Is it possible to create a spring-boot custom parent pom this is also a multi module project?

I would like to know how to set up a parent pom that handles my spring-boot dependencies as well as builds a few client jars.
Parent Project
|- client-jar-1
| |- src/main/java/MyPojo.java
| |- pom.xml (parent super pom? <package> type is jar)
|- pom.xml (parent is spring-boot-starter-parent <package> type is pom)
Child Project
|- pom.xml (parent pom is custom parent-project, includes client-jar-1 as a dependency)
My thought process is the the parent project can handle common dependencies across multiple independent spring applications and also build a few custom client jars or utility jars, that the child project could include at their own discretion.
Here is my first attempt at my pom files:
Parent Project POM
<?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.me.spring.boot.parent</groupId>
<artifactId>me-spring-boot-parent</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>pom</packaging>
<modules>
<module>directory_client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
client-jar-1 POM
<?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">
<parent>
<artifactId>me-spring-boot-parent</artifactId>
<groupId>com.me.spring.boot.parent</groupId>
<version>1.0.0.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.me.directory.cleint</groupId>
<artifactId>directory-client</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>jar</packaging>
</project>
Child Project 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>
<groupId>com.me</groupId>
<artifactId>child-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.me.spring.boot.parent</groupId>
<artifactId>me-spring-boot-parent</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
<properties>
<flyway.url>jdbc:postgresql://localhost:5432/db</flyway.url>
<flyway.driver>org.postgresql.Driver</flyway.driver>
<flyway.sqlMigrationSeparator>.sql</flyway.sqlMigrationSeparator>
<flyway.sqlMigrationSuffixes></flyway.sqlMigrationSuffixes>
</properties>
</project>
In my Child project when I try and access the client-jar-1 MyPojo I get a compilation error. Here is the expanded dependency from the child project
I can even get my IDE to import the MyPojo class, but there is a problem with the packages and it can't truly resolve the class.
Yes it is possible, in your parent pom add :
<modules>
<module>child1</module>
<module>child2</module>
</modules>
And in your child1 and child2 pom add :
<parent>
<groupId>yourorganization</groupId>
<artifactId>parent</artifactId>
</parent>
The parent pom will contain the spring-boot-starter-parent

spring boot multi modules package

I am trying to use Maven to package Spring Boot with multi modules,Here my main module pom.xml :
<modules>
<module>my-data-services</module>
<module>my-message-services</module>
<module>my-common</module>
</modules>
<groupId>com.my</groupId>
<artifactId>my-multi-services</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>....</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
my-common pom.xml:
<parent>
<artifactId>my-multi-services</artifactId>
<groupId>com.my</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-common</artifactId>
<packaging>jar</packaging>
<dependencies>....</dependencies>
and my-data-services pom.xml:
<parent>
<artifactId>my-multi-services</artifactId>
<groupId>com.my</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-data-services</artifactId>
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>my-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
my-common module is just a common utils lib not a runnable module,but when i trying to mvn clean package,exception throws below:
Execution default of goal org.springframework.boot:spring-boot-maven-p
lugin:1.4.2.RELEASE:repackage failed: Unable to find main class
then i add a main class,this module can package,but its not a lib jar, its like a runnable spring boot jar
-BOOT-INF
|
-META-INF
|
-org
and the exception throws
Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:3.1:compile (default-compile) on project my-data-services: Compilation failure: Compilation failure: package com.my.common.utils does not exist;
com.my.common.utils is in module my-common
How do i fix this problem, and in spring boot multi modules project,how to package a common utils lib without BOOT-INF
This happens because your modules will have the 'spring-boot-maven-plugin' added to them because it's defined in their parent.
What you need to do is move everything into submodules, even your application starter class. How I do this usually:
my-parent-module
my-service-module
my-common-module
my-web-module (or my-runtime-module in a non-web app)
The my-parent-module will have 'spring-boot-starter-parent' as it's parent but it not going to have an src folder because everything is moved into submodules.
The my-web-module will depend on the other modules and will have the 'spring-boot-maven-plugin'. You will be able to run the app with 'mvn spring-boot:run' in the my-web-module folder.

Error when opening a new Spring Boot Project using SpringToolSuite

I am new to Spring.I downloaded SpringToolSuite 3.7.2 and i am trying to create a simple SpringBootApplication.
I go to File->New Spring Starter Project,Select Type as Maven project and Under dependencies i selected Web and click on Finish. I get an error on my parent tag in pom.xml and also not able to see the maven dependency libraries.
I tried right clicking on the project->Select Maven->Update Projects-> Checked all boxes and Update but does not work. I also tried cleaning the repository under C:{User}.m2 clean and rebuild the project but did not work for me.Screenshot of the STS
Project build error: Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:1.4.3.RELEASE from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org and 'parent.relativePath' points at no local POM
Please tell me what am i missing.
Below is my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringApplication1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The error suggests that you are not able to connect to (https://repo.maven.apache.org/maven2).
Please check your maven settings.xml (Window -> Preferences -> Maven -> Global/User Settings):
If you are using proxy, you need to add it on your settings.xml.
Something like this :
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
...
</settings>
As you can see on https://maven.apache.org/settings.html.
If you are not using proxy, be sure that you don't have any proxy configuration on your settings.

How to build an EAR project with EJB and WAR using Maven?

I tried to create EAR Project with EJB and WAR but I have some problem.
I created the main project from the Java EE 6 EAR Archetype:
<dependency>
<groupId>org.codehaus.mojo.archetypes</groupId>
<artifactId>ear-javaee6</artifactId>
<version>1.5</version>
</dependency>
Then I created the EJB module from the Java EE 6 EJB JAR Archetype:
<dependency>
<groupId>org.codehaus.mojo.archetypes</groupId>
<artifactId>ejb-javaee6</artifactId>
<version>1.5</version>
</dependency>
And then I created the second module from the Javax Faces WAR Archetype:
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-war-archetype</artifactId>
<version>2.2</version>
</dependency>
Then I added dependencies to main pom.xml:
<!-- Define the versions of your ear components here -->
<dependencies>
<dependency>
<groupId>QCforCC-main</groupId>
<artifactId>QCforCC-ejb</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>QCforCC-main</groupId>
<artifactId>QCforCC-war</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
And then I tried to build the project - using maven clean and instal .
But I have an error:
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='QCforCC-main:QCforCC-war:1.0-SNAPSHOT'}' and 'Vertex{label='QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT'}' introduces to cycle in the graph QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT --> QCforCC-main:QCforCC-war:1.0-SNAPSHOT --> QCforCC-main:QCforCC-ejb:1.0-SNAPSHOT - [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException
Process finished with exit code 1
And in EAR pom.xml I have:
<modules>
<module>QCforCC-ejb</module>
<module>QCforCC-war</module>
</modules>
<packaging>pom</packaging>
But if I change <packaging>pom</packaging> to <packaging>ear</packaging>
IDEA show error in popup:
Some problems were encountered while processing the POMs:
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-ear-plugin # line 41, column 21
[ERROR] 'packaging' with value 'ear' is invalid. Aggregator projects require 'pom' as packaging. # line 12, column 16
I highly suggest that you understand how multi-module builds work. The Sonatype book has a great chapter describing in great detail.
To build an EAR with an EJB and a WAR, you actually need three modules, for the EJB, WAR and EAR. The parent POM just holds everything together and has a packaging type of POM.
So the parent pom.xml should look like this:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>Multi Chapter Simple Parent Project</name>
<modules>
<module>ejb-module</module>
<module>war-module</module>
<module>ear-module</module>
</modules>
</project>
Then, each of the child POMs would look like this:
ejb-module/pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ejb-module</artifactId>
<packaging>ejb</packaging>
</project>
war-module/pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>war-module</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>
<dependencies>
<dependency>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>ejb-module</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
ear-module/pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>ear-module</artifactId>
<packaging>ear</packaging>
<name>EAR module</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<ejbModule>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>ejb-module</artifactId>
<bundleFilename>ejb-module.jar</bundleFilename>
</ejbModule>
<webModule>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>war-module</artifactId>
<contextRoot>/foo</contextRoot>
</webModule>
</configuration>
</plugin>
</plugins>
</build>
</project>
parent QCforCC-parent :
<artifactId>QCforCC-parent</artifactId>
<packaging>pom</packaging>
...
...
<modules>
<module>QCforCC-ear</module>
<module>QCforCC-ejb</module>
<module>QCforCC-war</module>
</modules>
QCforCC-ear :
<artifactId>QCforCC-ear</artifactId>
<packaging>ear</packaging>
...
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<version>5</version>
<displayName>XXXXXXXX</displayName>
<modules>
<webModule>
<moduleId>WebModule_XXX</moduleId>
<groupId>${project.groupId}</groupId>
<artifactId>>QCforCC-war</artifactId>
<contextRoot>XXXXXXXX</contextRoot>
</webModule>
<jarModule>
<groupId>${project.groupId}</groupId>
<artifactId>QCforCC-ejb</artifactId>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>

Resources