6

I want to compile test files using some external jar dependencies which will not be present in pom.xml's dependency tag. Is there any way through configuration. Something like this-

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <configuration>
        <classPathElements>
            <classPathElement>somejar.jar</classPathElement>
        </classPathElements>
      </configuration>
    </execution>

  </executions>
</plugin>
Disha
  • 822
  • 1
  • 10
  • 39
  • 1
    Just a little question. Do you need to compile your tests classes with external JARs, or do you need to execute tests with external JARs? – Eduardo Yáñez Parareda Nov 22 '16 at 07:31
  • Actually both , but for execution I am using maven-surefire-plugin.. for which there is option to pass external jars using tag – Disha Nov 22 '16 at 08:46
  • Why are you trying to do this? (Besides the fact that you can't). – Tunaki Nov 22 '16 at 09:10
  • @tunaki I have a different folder structure for my project and I am overriding most of the lifecycle phases of maven. Also , these jar dependencies will be project specific which will not be there in .m2 repo – Disha Nov 22 '16 at 09:14
  • @tunaki right now I am trying to resolve this issue by overriding test-compile phase – Disha Nov 22 '16 at 09:15
  • That raises even more question. Why are you overriding lifecycle phases, and what is the connection with this? You can define your own lifecycle, no need to override anything. And why are you trying to use JARs which are not in the local repository? – Tunaki Nov 22 '16 at 09:15
  • basically I have created my own junitTestSuit which will be using one of our product and it has multiple plugin dependencies.. which we can not install in .m2 repo.. so the need – Disha Nov 22 '16 at 09:27
  • That doesn't answer the previous questions. Why can't you install that in a local repo, or use a repository manager? And why are you overriding Maven default lifecycle? The point is, you cannot do what you're trying to do. You want to add JARs during compilation, but not declare that JAR as a dependency, which is going against the very notion of using Maven to begin with. It sounds like you may need to create your own plugins, with your own lifecycle, instead of trying to override things you cannot, by design. – Tunaki Nov 22 '16 at 09:46
  • Its our products requirement .. and yes.. I am creating my own lifecycle to resolve this requirement now.. – Disha Nov 22 '16 at 09:57
  • 1
    Approach like : [maven-add-a-dependency-to-a-jar-by-relative-path](http://stackoverflow.com/questions/2229757/maven-add-a-dependency-to-a-jar-by-relative-path/2230464#2230464) and [can-i-add-jars-to-maven-2-build-classpath-without-installing-them](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them#364188) can help you maybe. I think the second one is interesting in your if your problem is to not use the .m2 repo. – colin aygalinc Nov 22 '16 at 10:38

1 Answers1

0

Try this, it includes libraries from your project

<dependency>
        <groupId>mylib</groupId>
        <artifactId>com.mylib</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/mylib.jar</systemPath>
</dependency>