Exclude folder from dependency in maven - java

Is it possible to define the directory exclusion for the dependency in maven?
I have a dependency defined in pom.xml. When compiled, this dependency is added into META-INF/lib. The problem is, the library contains about 40 MB of XML files I do not need, so I want to get rid of them.
Something like this:
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<scope>test</scope>
<exclusions>
<exclusion>
<exclude>**/xml/*</exclude>
</exclusion>
...
This is what I want to use as a dependency:
Jira Func Tests
The folder with XMLs is in the compiled jar in META-INF/lib/jira-func-tests-6.4.6.jar/XML
Before compilation, the XML folder is in resources folder of the jira-func-tests I guess?

those XML files have to be "attached to something". Here is an example what usualy works and should work even in maven-2. So finding connections between those XML files and groupIDs and artifactIDs.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>

Related

Maven dependency bcprov issue

I have bouncy castle dependency used in my application and I want this dependency to be excluded in the pom.xml file. Even after removing this dependency from the pom file, it is still appearing in the m2 repository folder. My application is a Spring-MVC 5 framework and running on Tomcat version 9. While deploying there are two different versions of "bcprov". I need to exclude both bcprov-jdk14 and bcprov-jdk15on from the pom.xml file.
Below is the dependency in the pom file.
<dependency>
<groupId>bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
<version>140</version>
</dependency>
I also tried another link for the exclusion but it did not work - Maven Transitive Dependency issue
Please help how to exclude the above dependencies.
To exclude some transitive dependencies you just have to add <exclusion> tags in your POM file. You need to remove the bcprov-jdk14 artifact from every dependency that uses it (check the dependency tree), e.g.:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
<exclusions>
<exclusion>
<groupId>bouncycastle</groupId>
<artifactId>bcmail-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bctsp-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
Remember however to add the correct versions of those dependencies:
<properties>
...
<bc.version>1.69</bc.version>
</properties>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>${bc.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>${bc.version}</version>
</dependency>
</dependencies>
Alternatively don't use the features of the libraries that depend on BouncyCastle (signature verification/encryption).

AbstractMethodError due to slf4j api and sl4j marker call apacheds-all-1.5.5

I landed on the famous error:
java.lang.AbstractMethodError: ch.qos.logback.classic.Logger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:120)
After going thru a lot of SO questions here is my progress:
mvn.cmd dependency:tree -DskipTests | findstr /R /C:slf4j proof that I'm using the same versions of slf4j on slf4j-api, jcl-over-slf4j (1.5.5, I can't use later versions coz of the following)
looking for the classpath where some Classes are loaded from we see the following results:
debugJars(org.slf4j.spi.LocationAwareLogger.class);
debugJars(org.apache.commons.logging.impl.SLF4JLocationAwareLog.class);
debugJars(org.slf4j.Marker.class);
debugJars(ch.qos.logback.classic.Logger.class);
Returns:
file:/C:/Users/User/.m2/repository/org/apache/directory/server/apacheds-all/1.5.5/apacheds-all-1.5.5.jar!/org/slf4j/spi/
file:/C:/Users/User/.m2/repository/org/slf4j/jcl-over-slf4j/1.5.5/jcl-over-slf4j-1.5.5.jar!/org/apache/commons/logging/impl/
file:/C:/Users/User/.m2/repository/org/apache/directory/server/apacheds-all/1.5.5/apacheds-all-1.5.5.jar!/org/slf4j/
file:/C:/Users/User/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/ch/qos/logback/classic/
How to get of rid of the fact that apacheds-all override my correct sl4j dependencies?
pom.xml doesn't include anything about appacheds-all dependency I understand that it may be an implicit dependency but how can I solve this?
I am also using the same dependency, I am not facing any issues
Can you try the following
<org.slf4j-version>1.5.5</org.slf4j-version>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>

Maven: Current project and dependent project use different version of the same library

The pom for my main project declares a version of hadoop-client. A dependency declares a different version of hadoop-client. Which one actually ends up being used?
pom.xml for my main project:
<dependency>
<groupId>com.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.myown.group</groupId>
<artifactId>my-own-artifact</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
pom.xml for the library that my main project depends on:
<dependency>
<groupId>com.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.5.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Maven has a complex set of rules for version resolution, summarized as 'nearest wins'. One source of details is this post here. You can find out what Maven decided by using mvn dependency:list or mvn dependency:tree.

Jsp servlet exception

I've seen some similar answer to this question perhaps but I feel my situation is different. I'm developing a spring MVC app working great so far, that is I included hadoop api in my project, when I included hadoop this exception started happening when I am trying to open the initial dashboard page which worked previously :
java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/
ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
org.apache.jsp.ServerInfo_jsp._jspInit(ServerInfo_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:158)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:9
Here is how my hadoop dependency looks like :
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>0.23.1-mr1-cdh4.0.0b2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.23.1-mr1-cdh4.0.0b2</version>
<exclusions>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
Once I add this to my app it's no longer usable and naturally without these dependecies things run very smooth. What am I missing here?
I think that your problem is that hadoop is including a version of the servlet API which is coming before the "correct" servlet API in the classpath. Hadoop has dependencies on Jetty, and jetty will in turn try to include a servlet API.
I have a project with a very similar setup, Spring MVC and Hadoop. I have the below exclusions for the hadoop dependency. Note that this might vary slightly depending on your hadoop distribution, I am using cloudera's. Since the servlet container you are using will usually ship with its own javax.servlet dependency, your exclusion needs to catch this case. I have only tested the below configuration with Jetty:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>jasper-compiler</artifactId>
</exclusion>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>jasper-runtime</artifactId>
</exclusion>
<!-- other exclusions snipped for brevity -->
I recently had a similar experience with hadoop-core 1.2.1 in my web application. On top of the dependencies Paul Sanwald indicated, I excluded some org.eclipse.jetty dependendencies:
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
</exclusion>
It's not completely clear to me whether every one of these had to be excluded, but I wanted to be sure no unneeded dependencies were getting in - I'm using Tomcat, which provides JSP and servlet libraries. jetty-servlet definitely had to go.

Maven dependency log4j error

Hello everyone I have error which breaks up my build for no reason, here is the error message :
error: error reading
/.m2/repository/com/sun/jdmk/jmxtools/1.2.1/jmxtools-1.2.1.jar;
error in opening zip file error: error
reading
/.m2/repository/com/sun/jmx/jmxri/1.2.1/jmxri-1.2.1.jar;
error in opening zip file
I'm using this dependency :
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<scope>provided</scope>
</dependency>
How can I fix this ?
You most likely don't need jmxtools or jmxri, so you can probably exclude them from your dependencies:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>
Seriously, these dependencies on JMX, JMS, Java Mail are ridiculous and having to deal with exclusions to do some logging makes me speechless. So, I'd rather use the previous version of log4j (1.2.14) or just switch to logback.

Categories