Exclude files from a submodule using maven is not working - maven

I have a multi-module project is the following structure:
ROOT
Module A
-- A_pom.xml
Module B depends on Module A
-- src
--- com.mycompany
---- App 1
---- App 2
-- B_pom.xml
-- B_App1_pom.xml
-- B_App2_pom.xml
pom.xml <!-- default parent pom fiel -->
second_parent_pom.xml
The 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>Parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>module-A</module>
<module>module-B/</module>
</modules>
</project>
From ROOT directory mvn package helped me to build both jar files A.jar and B.jar (this include App 1 and App 2)
Now I am looking a way to build both jar files but that will include only App 1. But I have tried the following code but no result.
<build>
[...]
<resources>
<resource>
<directory>${basedir}</directory>
<excludes>
<exclude>/../Module_B/src/com/mycompany/App2</exclude>
</excludes>
</resource>
</resources>
</build>
When I try to specify the B_App2_pom.xml from ROOT directory, it is still pointing to "pom.xml" which make me get the same 2 jar files A.jar and B.jar instead of A.jar and B_App2.jar.
Also, I have tried to create another parent pom file that
The Second Parent 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>company</groupId>
<artifactId>SecondParent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>SecondParent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>module-A/B_App1_pom.xml</module>
<module>module-B/B_App2_pom.xml</module>
</modules>
</project>

Related

Adding IBMs wala.cast.python.ml as dependency

I am trying to build a project that has this dependency. The corresponding entry in the build.gradle file is like this.
implementation group: 'com.ibm.wala', name:'com.ibm.wala.cast.python.ml', version:'0.0.1-SNAPSHOT'
When I run the command ./gradlew build it failed to find the dependency. There is no point of searching the remote maven, because it is not there. The dependency is a GitHub repo. The error says it looks for two local locations as well.
- file:/home/<user>/.m2/repository/com/ibm/wala/com.ibm.wala.cast.python.ml/0.0.1-SNAPSHOT/maven-metadata.xml
- file:/home/<user>/.m2/repository/com/ibm/wala/com.ibm.wala.cast.python.ml/0.0.1-SNAPSHOT/com.ibm.wala.cast.python.ml-0.0.1-SNAPSHOT.pom
But, when I browse the content in .m2/repository/com/ibm/wala it has a directory ML which has the following.
0.0.1-SNAPSHOT
|--maven-metadata-local.xml
|--ML-0.0.1-SNAPSHOT.pom
maven-metadata-local.xml
maven-metadata-local.xml
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>com.ibm.wala</groupId>
<artifactId>ML</artifactId>
<versioning>
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20221028162537</lastUpdated>
</versioning>
</metadata>
ML-0.0.1-SNAPSHOT.pom
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.wala</groupId>
<artifactId>ML</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project-version>0.0.1-SNAPSHOT</project-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<build-alias>b000</build-alias>
</properties>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<modules>
<module>com.ibm.wala.cast.python</module>
<module>com.ibm.wala.cast.python.jython</module>
<module>com.ibm.wala.cast.python.jython3</module>
<module>com.ibm.wala.cast.python.test</module>
<module>com.ibm.wala.cast.python.jython.test</module>
<module>com.ibm.wala.cast.python.jython3.test</module>
<module>com.ibm.wala.cast.python.ml</module>
<module>com.ibm.wala.cast.python.ml.test</module>
</modules>
</project>
I tried to hack by creating a dir com.ibm.wala.cast.python.ml, then copying the content in ML to it. But it does not work.
Any ideas on how to include this dependency?

Maven claims Cyclic references but none exist

Here is my parent module 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>io.paratek.dynalib</groupId>
<artifactId>dynalib</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dynanode</module>
</modules>
<dependencies>
<dependency>
<groupId>io.paratek.dynalib</groupId>
<artifactId>dynanode</artifactId>
<version>1.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Here is the child
<?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>
<artifactId>dynalib</artifactId>
<groupId>io.paratek.dynalib</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dynanode</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
As you see my child has ZERO dependencies, however, maven is spitting out
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='io.paratek.dynalib:dynalib:1.0-SNAPSHOT'}' and 'Vertex{label='io.paratek.dynalib:dynanode:1.1-SNAPSHOT'}' introduces to cycle in the graph io.paratek.dynalib:dynanode:1.1-SNAPSHOT --> io.paratek.dynalib:dynalib:1.0-SNAPSHOT --> io.paratek.dynalib:dynanode:1.1-SNAPSHOT
I cannot get maven to recognize 'dynanode' as a dependency in 'dynalib'. There are no errors in the pom file yet I can't use dynanode functions in dynalib.
Recreated the project and everything works, poms are the same.

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

'Go to definition' in a multi-module maven project with module dependencies in IntelliJ

I have a parent module with two child modules (module1 and module2)
module2 has a maven dependency to module1
In module2 I reference a class that's in module1
When I try to jump to the definition of this class, IntelliJ opens the .class file in my maven repository. I expect it to jump to the java class in module1. How can I get IntelliJ to jump this class file?
I'm running IntelliJ IDEA Ultimate 14.0.1
parent 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>test</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>module2</module>
<module>module1</module>
</modules>
</project>
module1 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>test</artifactId>
<groupId>test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
</project>
module2 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>test</artifactId>
<groupId>test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module2</artifactId>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>module1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
This other answer over here at stackoverflow helped me. You essentially have to add module dependencies if they're not already in place.
You have two options:
Way #1:
Build module1 with sources. This way ensures that you will have always consistent source code to used java classes.
To enable build with sources add this to maven plugins:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
Way #2:
Add your module1 to module2 via Project structure > Modules > Dependencies > + > 3. Module dependency
Well, it wont be as consistent as way #1 but its much faster to setup, if you are using maven builds. If you are using intellij idea build you can mark this dependency as export to include it in your module1 output dir.

How to override maven property in command line?

I have the following plain pom running by Maven 3.0.4.
<?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>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
I am trying to override default settings in command line like this:
mvn -Dproject.build.finalName=build clean package
But this this is ignored, and I get test-1.0.jar. I've tried to change another properties, like outputDirectory, directory, artifactId, but also failed.
What is the proper way to do this thing?
See Introduction to the POM
finalName is created as:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
One of the solutions is to add own property:
<properties>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<build>
<finalName>${finalName}</finalName>
</build>
And now try:
mvn -DfinalName=build clean package

Resources