Can I add jars to maven 2 build classpath without installing them?

前端 未结 24 2581
萌比男神i
萌比男神i 2020-11-22 01:41

Maven2 is driving me crazy during the experimentation / quick and dirty mock-up phase of development.

I have a pom.xml file that defines the dependenc

相关标签:
24条回答
  • 2020-11-22 02:09

    After having really long discussion with CloudBees guys about properly maven packaging of such kind of JARs, they made an interesting good proposal for a solution:

    Creation of a fake Maven project which attaches a pre-existing JAR as a primary artifact, running into belonged POM install:install-file execution. Here is an example of such kinf of POM:

     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>image-util-id</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${basedir}/file-you-want-to-include.jar</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    But in order to implement it, existing project structure should be changed. First, you should have in mind that for each such kind of JAR there should be created different fake Maven project (module). And there should be created a parent Maven project including all sub-modules which are : all JAR wrappers and existing main project. The structure could be :

    root project (this contains the parent POM file includes all sub-modules with module XML element) (POM packaging)

    JAR 1 wrapper Maven child project (POM packaging)

    JAR 2 wrapper Maven child project (POM packaging)

    main existing Maven child project (WAR, JAR, EAR .... packaging)

    When parent running via mvn:install or mvn:packaging is forced and sub-modules will be executed. That could be concerned as a minus here, since project structure should be changed, but offers a non static solution at the end

    0 讨论(0)
  • 2020-11-22 02:10

    If you want a quick and dirty solution, you can do the following (though I do not recommend this for anything except test projects, maven will complain in length that this is not proper).

    Add a dependency entry for each jar file you need, preferably with a perl script or something similar and copy/paste that into your pom file.

    #! /usr/bin/perl
    
    foreach my $n (@ARGV) {
    
        $n=~s@.*/@@;
    
        print "<dependency>
        <groupId>local.dummy</groupId>
        <artifactId>$n</artifactId>
        <version>0.0.1</version>
        <scope>system</scope>
        <systemPath>\${project.basedir}/lib/$n</systemPath>
    </dependency>
    ";
    
    0 讨论(0)
  • 2020-11-22 02:10

    For those that didn't find a good answer here, this is what we are doing to get a jar with all the necessary dependencies in it. This answer (https://stackoverflow.com/a/7623805/1084306) mentions to use the Maven Assembly plugin but doesn't actually give an example in the answer. And if you don't read all the way to the end of the answer (it's pretty lengthy), you may miss it. Adding the below to your pom.xml will generate target/${PROJECT_NAME}-${VERSION}-jar-with-dependencies.jar

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                      <manifest>
                        <mainClass>my.package.mainclass</mainClass>
                      </manifest>
                    </archive>
    
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <!-- bind to the packaging phase -->
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
    
    0 讨论(0)
  • 2020-11-22 02:10

    This doesn't answer how to add them to your POM, and may be a no brainer, but would just adding the lib dir to your classpath work? I know that is what I do when I need an external jar that I don't want to add to my Maven repos.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 02:17

    You really ought to get a framework in place via a repository and identifying your dependencies up front. Using the system scope is a common mistake people use, because they "don't care about the dependency management." The trouble is that doing this you end up with a perverted maven build that will not show maven in a normal condition. You would be better off following an approach like this.

    0 讨论(0)
  • 2020-11-22 02:18

    Note: When using the System scope (as mentioned on this page), Maven needs absolute paths.

    If your jars are under your project's root, you'll want to prefix your systemPath values with ${basedir}.

    0 讨论(0)
提交回复
热议问题