ignore maven dependency during specific execution

2019-07-25 13:49发布

问题:

I have a (custom) embedded jetty launcher which I had been using to develop/test my web application (before moving to maven).

I am able to trick m2eclipse into putting the maven managed dependencies to libraries for the war file onto the launcher classpath (when run from eclipse). I did this by creating two dependencies for the same artifact: one of the type "war" and one the type "jar" ("jar" dependence tricks m2eclipse into doing what I want).

However the maven-assembly-plugin for the launcher fails when it cannot find the jar for the war project.

Q: Can I tell maven to ignore the jar dependency when running the package goal?

more detailed background: I have a webapp.war project and a jetty-launcher project, before moving to maven; I had an eclipse project dependency so that the launcher had all the war dependencies at runtime. Everything ran right out of the ide (with no re-build of the warfile) needed to test changes.

After moving to maven, this approach was outsmarted since m2eclipse knows not to include the libraries that the jar depends on (since they are war scoped).

回答1:

I got around this by using a seperate .m2/settings-eclipse.xml (for eclipse) where I over-ride the default type of dependency. so in my launcher POM, I have:

<dependency>
  <groupId>com.myco</groupId>
  <artifactId>my-server</artifactId>
  <version>${project.version}</version>

  <!-- here we want eclipse to see "jar" but command line to see "war" -->
  <type>${jetty.launcher.workaround}</type>
</dependency>

I still would like to know if there is a direct way to approach the original problem.



回答2:

It's probably too late, but this can be helpful for someone:

<optional>true</optional>
</dependency>

So, add <optional>true</optional> to your pom.xml