Have a Pom.xml Download and Reference a Parent Pom - maven

I have a Pom.xml I was wondering if there is a way to have the Pom download the Parent pom from somewhere then reference the parent Pom.
Edit: I'm having issue with the parent pom, this is the parent Pom I have
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev</groupId>
<artifactId>company-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<distributionManagement>
<repository>
<id>Artifactory</id>
<name>Artifactory-releases</name>
<url>http://artifactory:8081/artifactory/dev</url>
</repository>
<snapshotRepository>
<id>Artifactory</id>
<name>Artifactory-snapshots</name>
<url>http://artifactory:8081/artifactory/dev</url>
</snapshotRepository>
</distributionManagement>
</project>
I have uploaded the parent Pom.xml to artifactory, here is my 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>maven.Test</groupId>
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Artifactory</name>
<url>http://artifactory:8081/artifactory/webapp/home.html?0</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>dev</groupId>
<artifactId>company-parent</artifactId>
<version>1.0.0</version>
</parent>
</project>
But when I run it I get this error
[ERROR] Non-resolvable parent POM for maven.Test:Hello:1.0-SNAPSHOT: Could n
ot find artifact dev:company-parent:pom:1.0.0 in central (https://repo.maven.a
pache.org/maven2) and 'parent.relativePath' points at wrong local POM # line 22,
column 9 -> [Help 2]
How do I have my Pom find the artifact in my artifactory instance?

You parent pom should be pom with packaging type of pom. e.g:
<?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>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
</project>
The pom, mentioned above, you can deploy to artifactory and after that you can reference it as parent pom in the child pom. e.g:
<?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>parent</artifactId>
<groupId>org.example</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<repositories>
<repository>
<id>Artifactory-Releases</id>
<url>http://artifactory:8081/artifactory/dev</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>Artifactory-Snapshots</id>
<url>http://artifactory:8081/artifactory/dev</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>

Related

Stop Maven downloading plugins from private repository?

We have a company Nexus repository which has just a couple of private libraries but it mostly mirrors the public Maven Nexus repository.
The repository is on a private network which can only be accessed by VPN.
This is accessed using a settings.xml config like so (redacted)
User Settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>FOOSNAPSHOTS</id>
<username>foo-publisher</username>
<password>12345</password>
</server>
<server>
<id>BARRELEASES</id>
<username>bar-publisher</username>
<password>12345</password>
</server>
</servers>
<mirrors>
<mirror>
<id>foo-nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://foo/repository/foo-maven-group/</url>
</mirror>
<mirror>
<id>bar-nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://foo/repository/bar-maven-group/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>allow-snapshots</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://foo/repository/hosted-foo-snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>foo-snapshots-repo</id>
<url>https://foo/repository/hosted-bar-snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
In a sample spring boot project, when trying to build a project when not connected to the VPN it fails because it cannot get plugins like the following.
[ERROR] No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/bobbytables/.m2/repository), foo-nexus (https://foo/repository/foo-maven-group/)] -> [Help 1]
This is annoying as it then requires to be on the VPN, is there a way to get Maven to download plugins from public repos?
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>
<parent>
<groupId>foo.shared-maven-parent</groupId>
<artifactId>shared-maven-parent-java</artifactId>
<version>1.202211110800.52-x6937aa5.master</version>
</parent>
<groupId>foo.bar-service</groupId>
<artifactId>bar-service</artifactId>
<packaging>pom</packaging>
<version>3.0-SNAPSHOT</version>
<name>bar-service</name>
<description>Foo Bar - Bar is Foo</description>
<modules>
<module>contract</module>
<module>server</module>
</modules>
<properties>
<mcu.version>${project.version}</mcu.version>
<!-- Project's contract version -->
<mcu.project.contract.version>1.202204111324.39-x6bb54a3.master</mcu.project.contract.version>
</properties>
</project>
Service 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>
<parent>
<groupId>foo.bar-service</groupId>
<artifactId>bar-service</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>
<groupId>foo.bar/groupId>
<artifactId>server</artifactId>
<packaging>jar</packaging>
<name>Listing-command Server</name>
<description>Listing-command Server</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>foo.shared-maven-bom</groupId>
<artifactId>shared-maven-bom-chassis</artifactId>
<version>1.202211160925.72-xa9bdde9.master</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>foo.shared-commons.spring-boot</groupId>
<artifactId>jetty-server</artifactId>
<version>1.202211110857.90-xcb97a1c.master</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- Starter for Stackdriver Trace -->
</project>
How can I specify either at project or user level that I want plugins to download from the public repository?
Thanks

Discord JDA Dependency not found

I am trying to build a discord application but it can't access the JDA latest version.
Error:
Dependency 'net.dv8tion:JDA:4.2.1_255' not found
<?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>org.example</groupId>
<artifactId>Discord_BOT</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.1_255</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>
</project>

Not able to add parent tag in pom.xml maven

Not able to add parent tag in pom.xml just like below. It keeps giving me an error saying "Project 'org.springframework.boot:spring-boot:2.3.2.RELEASE' not found
Inspection info: Inspects a Maven model for resolution problems." Could someone help please?
<?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>org.learning.springboot</groupId>
<artifactId>SpringBootApplication</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
</dependency>
</dependencies>
</project>
Check first if repository is part of your project:
On maven central, I see:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
So, as in here, add:
<project>
<!------ others lines -->
<pluginRepositories>
<pluginRepository>
<id>repository.spring.release</id>
<name>Spring GA Repository</name>
<url>https://repo.spring.io/plugins-release/</url>
</pluginRepository>
</pluginRepositories>
</project>
Or, in your case, a repository, as in the same thread:
<repository>
<id>repository.springframework.maven.release</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://maven.springframework.org/milestone/</url>
</repository>

Maven Wont Change Artifactory Directory

I've had a fair amount of help with my Maven code and I'm currently trying to have my project deploy to a project determined by a variable. Here is my 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>maven.Test</groupId>
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Artifactory</name>
<url>http://artifactory:8081/artifactory/webapp/home.html?0</url>
<repositories>
<repository>
<id>parentPomLocation</id>
<name>Artifactory</name>
<url>http://artifactory:8081/artifactory/dev</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>dev</groupId>
<artifactId>company-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>
So my pom first pulls the parent Pom.xml from artifactory and checks it for it's distribution management. Here is the parent 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev</groupId>
<artifactId>company-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<distributionManagement>
<repository>
<id>Artifactory</id>
<name>Artifactory-releases</name>
<url>http://artifactory:8081/artifactory/${repo}</url>
</repository>
<snapshotRepository>
<id>Artifactory</id>
<name>Artifactory-snapshots</name>
<url>http://artifactory:8081/artifactory/${repo}</url>
</snapshotRepository>
</distributionManagement>
</project>
The parent Pom.xml's distribution url is determined through a variable I assign, this is the command line I run.
mvn deploy -D$repo=integration
But no matter what it always deploys to dev and not integration.

how do I configure the dependency among my local jars with maven?

I have configured a local repository and have some jars on it. Through the pom file, I used them but when I execute the mvn package phase, I'm seeing...
[WARNING] The POM for p-unit:p-unit:jar:0.15.319 is missing, no dependency information available
[WARNING] The POM for p-unit:p-unit-extension:jar:0.15.319 is missing, no dependency information available
I guess that maven didn't found the jars in the maven repo, so then it uses my local repo because the package phase ends fine.. but I would like to configure the dependency among my local jars (the p-unit-extension.jar depends on the p-unit.jar). How can I configure this in maven?
this is my 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.test</groupId>
<artifactId>perf-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>punit-test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>snapshots</id>
<url>file:///D:/Users/jmann/.m2/local</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>p-unit</groupId>
<artifactId>p-unit</artifactId>
<version>0.15.319</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>p-unit</groupId>
<artifactId>p-unit-extension</artifactId>
<version>0.15.319</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I would like to put only the p-unit-extension dependency so that maven downloads the p-unit jar automatically. Any error in my pom, please tell me.
this is my settings.xml:
<settings xmlns="maven.apache.org/SETTINGS/1.1.0"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="maven.apache.org/SETTINGS/1.1.0 maven.apache.org/xsd/settings-1.1.0.xsd">;
<localRepository>D:/Users/jmann/.m2/local</localRepository>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<activeProfiles/>
<pluginGroups/>
</settings>

Resources