Maven dynamically exclude class with same name from different dependencies -


there 2 classes com.package.a, 1 coming from

<dependency>     <groupid>com.package</groupid>     <artifactid>art1</artifactid> </dependency> 

and 1 coming from

<dependency>     <groupid>com.package</groupid>     <artifactid>art2</artifactid> </dependency> 

notice artifact ids different.

for different maven profiles, want exclude 1 version , keep other version. using shade plugin.

with maven-shade-plugin, possible exclude class specific dependencies. configured of filters property:

archive filters used. allows specify artifact in form of composite identifier used artifactset , set of include/exclude file patterns filtering contents of archive added shaded jar.

in case, exclude class com.package.a dependency art2, can have:

<filters>   <filter>     <artifact>com.package:art2</artifact>     <excludes>       <exclude>com/package/a.class</exclude>     </excludes>   </filter> </filters> 

to make dynamic, i.e. select @ build-time com.package.a class want keep, don't need use profile. can use maven property hold artifact id of dependency filter. in properties, add

<properties>   <shade.exclude.artifactid>art2</shade.exclude.artifactid> </properties> 

the shade.exclude.artifactid property hold artifact id of dependency filter. default, configuration select art2. then, in <filter> configuration of shade plugin, can use <artifact>com.package:${shade.exclude.artifactid}</artifact>.

here's full configuration of in action:

<build>   <plugins>     <plugin>       <artifactid>maven-shade-plugin</artifactid>       <version>2.4.3</version>       <executions>         <execution>           <id>shade</id>           <goals>             <goal>shade</goal>           </goals>           <phase>package</phase>           <configuration>             <filters>               <filter>                 <artifact>com.package:${shade.exclude.artifactid}</artifact>                 <excludes>                   <exclude>com/package/a.class</exclude>                 </excludes>               </filter>             </filters>           </configuration>         </execution>       </executions>     </plugin>   </plugins> </build> <properties>   <shade.exclude.artifactid>art2</shade.exclude.artifactid> </properties> 

running mvn clean package create uber jar a.class art1 since 1 art2 excluded. , then, running mvn clean package -dshade.exclude.artifactid=art1 keep time a.class dependency art2 since 1 art1 excluded.


Comments