21

I have to unpack some dependencies from a remote location and install them locally.

I successfully get them (using the antrun plugin) and install them (using the install plugin)

However, if I define them as dependencies (<dependency>..</dependency>) maven first tries to resolve them, and only then, if succeeds, proceeds to the antrun and install.

I also tried the build-helper-plugin and its attach-artifact, but it doesn't do anything (it doesn't even add the artifacts to the final war file)

So, how to run my executions before maven attempts to resolve dependencies?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140

4 Answers4

8

Interesting question! One attempt to (maybe) achiv this is to use a Multi Module Project setup.

Fetch and install your dependencies in the parent project and use them in the child module. I tested my assumption like this:

Parent pom.xml

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foo.bar</groupId>
    <artifactId>foo.bar.parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>foo.bar.child</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <configuration>
                            <target>
                                <echo
                                    message="Hello world!" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Module pom.xml

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foo.bar</groupId>
    <artifactId>foo.bar.child</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>foo.bar</groupId>
        <artifactId>foo.bar.parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <artifactId>can.not</artifactId>
            <groupId>find.me</groupId>
            <version>0.0.0</version>
        </dependency>
    </dependencies>

</project>

I get the "Hello World" output from the parent pom build before the child module build fails (because of missing dependency). If you achiev to provide the dependency in the parent.pom's build the child module project should be buildable.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 1
    good idea. I'll try it tomorrow (although it introduces a bit of complexity in an otherwise simple configuration) – Bozho May 10 '11 at 15:55
  • 1
    Maven 3.5.0 does not have this behavior, dependencies are always resolved first. – FUD Jan 23 '18 at 13:18
  • Want to comment, it actually does work however if you have a dependency defined in dependencyManagement with scope import it resolved dependency first. – FUD Jan 24 '18 at 04:58
1

you should look at running Artifact Repository Manager such as Archiva, then you can just load them in there, and in your ~/.m2/settings.xml file add you Archiva server and don't have to worry about doing local installs manually, that is assuming the artifacts aren't already in a remote repository. If your "remote location" is a Maven repository, Archiva can proxy that transparently as well.

  • 1
    Thanks, I'm aware of that option, but the project is expected to be short-lived, and the organizational effort to setup a repository will not be worth it. :) – Bozho May 10 '11 at 15:54
  • It will save you work in the long run when the short term project becomes the zombie project that never dies completely and every person that touches it has to **manually** add dependencies locally, and then you start not being able to find the correct ones because your project won't compile against newer versions, **it took me 5 mins to un-tar Archiva and start adding things to it.** And none of my developers has **ever** had to install a dependency locally since I set it up. –  May 10 '11 at 17:30
  • I agree with you. But we need to have a server for that, and admins to look at it... And there might be different versions of the dependencies for different environments (and different builds), so it will turn out more complicated anyway. It's not that I like what I'm saying, but arranging infrastructure for something that "could've been done with ant" .. you know ;) – Bozho May 10 '11 at 17:52
  • I run Archiva on a Virtual Machine that my team controls so that we can do what we want with it, all our development tools like this are on this one VM, all our IT department does is ensure the VM gets backed up every night. –  May 11 '11 at 16:18
  • 1
    I upvoted your answer, because it is in most cases the right thing to do. But I did it differently because of other internal complications. – Bozho May 11 '11 at 16:21
1

I ended up doing it in two phases:

  • setup the antrun and install executions to run on clean
  • when package is chosen, the build would fail if it hasn't been cleaned at least once

The solution introduces a bit of complexity though.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
-1

You can try the system scope in dependency and add systemPath:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/myhackedjunit-4.0.jar</systemPath>
</dependency>

The system scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

The systemPath is used only if the the dependency scope is system. The path must be absolute. Since it is assumed that system scope dependencies are installed a prior, Maven will not check the repositories for the project, but instead checks to ensure that the file exists.

kavai77
  • 6,282
  • 7
  • 33
  • 48