4

there

I met a weird problem. I have a multi-modules enterprise project that is built in Maven. I setup the project hierarchy like this

parentPom
   --MyEar (packaging ear)
   --MyUtilJar (packaging jar)
   --MyEJB (packing ejb)
   --MyWeb (packaging war)

In the MyEJB project, the pom.xml actually binds the apt plugin to the generate-sources phase to generate some java codes. MyEJB depends on MyUtilJar project.

My problem is that when I execute the mvn clean compile, everything works fine. But if I execute mvn clean generate-sources instead, it throws error, complaining it cannot resolve dependency for artifact mygroup:MyUtilJar:jar:1.0.

How can I solve this issue?

Big Dude
  • 264
  • 1
  • 6
  • 25
Lan
  • 6,470
  • 3
  • 26
  • 37

1 Answers1

0

In order for the generate-sources to work, you need to have all dependencies in a repository - your local one or a remote one. Just having the dependency in a folder near where it's needed won't work.

Try building and installing the until to put it in your local repository then running the generate-sources.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
  • 1
    Thanks for the reply. I kind of understand what you say, but my problem is that the whole project is a multi-module enterprise project. We compile/package the project at the parent project level. If I have 3 jar module projects that the MyEJB project depends on, are you saying I have to compile these 3 jar project one by one, package them and install them into the local repository? After all these steps are done, then I can run the generate-sources phase at the parent level? That sound really a hassle and counter-intuitive. – Lan Oct 17 '12 at 20:36
  • I don't think so. The parent pom calls out the other poms as modules, right? If so, you should be able to mvn:install the parent and then do the generate-sources. But maybe I'm assuming to much. Is it that you just moved the code to a new location and are trying to get any mvn command to work or have you successfully built the code but can't get generate-sources to work? – Chris Gerken Oct 17 '12 at 20:40
  • 1
    Understood. But my point is install phase is after generate-sources phase. So If I do mvn install at the parent level, the generate-sources phase has already been execute for all the sub-modules, so are the compile/package phases. It just sounds weird that we have to execute a later phase first so that we can execute an earlier phase again. Your solution probably will work, but it is not ideal. It takes more than 1 hour to package/install the whole project. What I am looking for is to generate source codes at the generate-sources phase without compile/package/install the whole project first. – Lan Oct 17 '12 at 21:00
  • Another point I try to make is that mvn clean compile works fine. There is no "install" phase executed yet. Why the "compile" phase works fine with MyUtilJar not in the local repository? – Lan Oct 17 '12 at 21:19
  • 1
    The need for running `mvn install` is usually a bad build smell. A Maven build is not following the *Maven way* if it cannot build with the following sequence of commands: `mvn clean; rm -rf ~/.m2/repository; mvn verify` I have chosen the sequence to be deterministic. If you need the last command to be `mvn install` that is not wrong... it's just pragmatic as long as you understand why you have made the choices forcing the install phase. – Stephen Connolly Oct 18 '12 at 11:37
  • 1
    @StephenConnolly Thanks for chiming in. My build has no problem following the sequence of commands you stated. It just mvn clean generate-sources have trouble locating the dependency. According to Chris, generate-sources requires all dependencies in the local or remote repository. I did not find such requirement in the maven documentation. If it is true, I don't understand why Maven put such an requirement to generate-sources phase, but not to compile or package phases. – Lan Oct 18 '12 at 18:39
  • You must have bound some plugin to that phase and that plugin may have the `@requiresDependencyResolution` annotation and therefore be ill-suited to binding to that phase. Never had the issue with well designed plugins in a maven build. – Stephen Connolly Oct 18 '12 at 19:53
  • @StephenConnolly You are right on the money! I found a similar post at http://jira.codehaus.org/browse/MNG-3283 talking about a similar problem and the post also mentions requiresDependencyResolution. In my case, I found out that the culprit in the antrun plugin. I need to execute an ant script to for source code generation at the generate-sources step. I cannot think of any workaround right now as the problem seems to be in the antrun plugin design itself. I could run the mvn install first, it just seems wrong that we have to run mvn install before mvn generate-sources. – Lan Oct 18 '12 at 22:34
  • Depends what you are doing with antrun. To be honest it's actually dead simple to write a maven plugin... 99 times out of 100 I find writing a maven plugin easier than using antrun. Where I hit issues is publishing the maven plugin, because prior to m3 it could not be part of the reactor... Though that would not solve your issue (as you'd need to release such a plugin first as using it from the reactor would require at least the package phase) – Stephen Connolly Oct 18 '12 at 23:08