5

First dependency is to validate xml file.

<dependency>
 <groupId>xerces</groupId>
 <artifactId>xerces</artifactId>
 <version>2.4.0</version>
</dependency>

Second dependency is to write POI object to file.

<dependency>
 <groupId>xerces</groupId>
 <artifactId>xercesImpl</artifactId>
 <version>2.12.0</version>
</dependency>

Now, I need a solution like this below :

<dependency>
 <groupId>xerces</groupId>
 <artifactId>xerces</artifactId> 
 <artifactId>xercesImpl</artifactId>
 <version>2.12.0</version>
</dependency>

Is it possible to achieve this ?

Anish B.
  • 9,111
  • 3
  • 21
  • 41

3 Answers3

2

It's not at all possible to keep multiple artifactId tags in a single dependency tag.

XML schema definition for Maven will not validate it. It will produce an error like this :

Project build error: Non-parseable POM project/pom.xml: Duplicated tag: 'artifactId' (position: START_TAG seen ...</artifactId>\n 

For example :

This is right.

<dependency>
 <groupId>xerces</groupId>
 <artifactId>xercesImpl</artifactId>
 <version>2.12.0</version>
</dependency>

This is wrong.

<dependency>
 <groupId>xerces</groupId>
 <artifactId>xerces</artifactId>
 <artifactId>xercesImpl</artifactId>
 <version>2.12.0</version>
</dependency>

You can simply use like this as artifactId are different (without any issue) :

    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xerces</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.12.0</version>
    </dependency>

You can refer to this : Multiple versions of the same dependency in Maven

Note : JDK 8 already gives you SAX parser to validate XML. Please use that. You don't need third party jar to validate XML.

Read here about Java SAX Parser.

Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    Thank you anish for replay , problem is first dependeny for validating xml file and send one is writing apache poi file.I need to achive both in single project –  Jun 01 '20 at 03:52
  • 1
    Multiple versions of the same dependency in Maven << this link artifactId id are same In my case artifactId are different. –  Jun 01 '20 at 03:54
  • @DileepBalineni What is the problem if you add both ? – Anish B. Jun 01 '20 at 03:56
  • The package org.xml.sax is accessible from more than one module: , java.xml this one is the issue –  Jun 01 '20 at 06:19
  • 1
    Ok, this is not about Maven dependencies but Java modules? – J Fabian Meier Jun 01 '20 at 07:31
  • @DileepBalineni Are you using java 8 ? If yes, then Java 8 already contains SAX parser. Use that. You don't need third party to validate xml. Read this : https://www.tutorialspoint.com/java_xml/java_sax_parser.htm – Anish B. Jun 01 '20 at 07:37
  • @DileepBalineni Is this helpful ? – Anish B. Jun 01 '20 at 16:51
0

Maven POM schema won't allow you to declare multiple artifacts of the same group in a single declaration. And to be honest, I see no strong motivation to do so, for some reasons:

  1. Once your POM is set up, you won't change it that often.

  2. There are cases where versions also vary between artifacts of the same group. Maven keeps the parsing simple by assuming that it's always different. The downside is that you have to duplicate some lines, but not a huge deal.

  3. It's unlikely that you have that many artifacts of the same group to justify that. Spring could be one of the exceptions, but they offer a Bill of Materials (BoM) that already do most of the heavy lifting.

Note 1: If you are looking for a more compact version of your build, you may have a look at Maven Polyglot that allows you to have more compact build files while still using Maven.

Note 2: An alternative to Maven that would allow for something like that is Gradle. Due to its imperative nature, you can just code it. Beware though, that moving between Maven and Gradle is a big leap, not just translating some scripts. Both tools have their space and should be considered carefully.

rph
  • 2,104
  • 2
  • 13
  • 24
0

I can see two issues that may concern you and could be the source of your need.

  1. excessive over-bloated POM file, sort of
  2. maintainability in case the groupId changes.

If you are concerned with the length and the readability of the POM file, you can try some Maven extension, like the maven polyglot (already mentioned), or some other with which you can write the POM using "thinXML format".

If you are concerned about maintainability, then you can define the groupId in property and reference that using maven's built-in ${} syntax.

Peter Verhas
  • 268
  • 1
  • 6