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

前端 未结 24 2577
萌比男神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 01:53

    I found another way to do this, see here from a Heroku post

    To summarize (sorry about some copy & paste)

    • Create a repo directory under your root folder:
    yourproject
    +- pom.xml
    +- src
    +- repo
    
    • Run this to install the jar to your local repo directory
    mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0
    
    • Add this your pom.xml:
    <repositories>
        <!--other repositories if any-->
        <repository>
            <id>project.local</id>
            <name>project</name>
            <url>file:${project.basedir}/repo</url>
        </repository>
    </repositories>
    
    
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>mylib</artifactId>
        <version>1.0</version>  
    </dependency>
    
    0 讨论(0)
  • 2020-11-22 01:53

    I just wanted a quick and dirty workaround... I couldn't run the script from Nikita Volkov: syntax error + it requires a strict format for the jar names.

    I made this Perl script which works with whatever format for the jar file names, and it generates the dependencies in an xml so it can be copy pasted directly in a pom.

    If you want to use it, make sure you understand what the script is doing, you may need to change the lib folder and the value for the groupId or artifactId...

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    open(my $fh, '>', 'dependencies.xml') or die "Could not open file 'dependencies.xml' $!";
    foreach my $file (glob("lib/*.jar")) {
        print "$file\n";
        my $groupId = "my.mess";
        my $artifactId = "";
        my $version = "0.1-SNAPSHOT";
        if ($file =~ /\/([^\/]*?)(-([0-9v\._]*))?\.jar$/) {
            $artifactId = $1;
            if (defined($3)) {
                $version = $3;
            }
            `mvn install:install-file -Dfile=$file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar`;
            print $fh "<dependency>\n\t<groupId>$groupId</groupId>\n\t<artifactId>$artifactId</artifactId>\n\t<version>$version</version>\n</dependency>\n";
            print " => $groupId:$artifactId:$version\n";
        } else {
            print "##### BEUH...\n";
        }
    }
    close $fh;
    
    0 讨论(0)
  • 2020-11-22 01:54

    This is what I have done, it also works around the package issue and it works with checked out code.

    I created a new folder in the project in my case I used repo, but feel free to use src/repo

    In my POM I had a dependency that is not in any public maven repositories

    <dependency>
        <groupId>com.dovetail</groupId>
        <artifactId>zoslog4j</artifactId>
        <version>1.0.1</version>
        <scope>runtime</scope>
    </dependency>
    

    I then created the following directories repo/com/dovetail/zoslog4j/1.0.1 and copied the JAR file into that folder.

    I created the following POM file to represent the downloaded file (this step is optional, but it removes a WARNING) and helps the next guy figure out where I got the file to begin with.

    <?xml version="1.0" encoding="UTF-8" ?>
    <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>com.dovetail</groupId>
        <artifactId>zoslog4j</artifactId>
        <packaging>jar</packaging>
        <version>1.0.1</version>
        <name>z/OS Log4J Appenders</name>
        <url>http://dovetail.com/downloads/misc/index.html</url>
        <description>Apache Log4j Appender for z/OS Logstreams, files, etc.</description>
    </project>
    

    Two optional files I create are the SHA1 checksums for the POM and the JAR to remove the missing checksum warnings.

    shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar \
              > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar.sha1
    
    shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom \
              > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom.sha1
    

    Finally I add the following fragment to my pom.xml that allows me to refer to the local repository

    <repositories>
        <repository>
            <id>project</id>
            <url>file:///${basedir}/repo</url>
        </repository>
    </repositories>
    
    0 讨论(0)
  • 2020-11-22 01:54

    Maven install plugin has command line usage to install a jar into the local repository, POM is optional but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff).

    0 讨论(0)
  • 2020-11-22 01:54

    Even though it does not exactly fit to your problem, I'll drop this here. My requirements were:

    1. Jars that can not be found in an online maven repository should be in the SVN.
    2. If one developer adds another library, the other developers should not be bothered with manually installing them.
    3. The IDE (NetBeans in my case) should be able find the sources and javadocs to provide autocompletion and help.

    Let's talk about (3) first: Just having the jars in a folder and somehow merging them into the final jar will not work for here, since the IDE will not understand this. This means all libraries have to be installed properly. However, I dont want to have everyone installing it using "mvn install-file".

    In my project I needed metawidget. Here we go:

    1. Create a new maven project (name it "shared-libs" or something like that).
    2. Download metawidget and extract the zip into src/main/lib.
    3. The folder doc/api contains the javadocs. Create a zip of the content (doc/api/api.zip).
    4. Modify the pom like this
    5. Build the project and the library will be installed.
    6. Add the library as a dependency to your project, or (if you added the dependency in the shared-libs project) add shared-libs as dependency to get all libraries at once.

    Every time you have a new library, just add a new execution and tell everyone to build the project again (you can improve this process with project hierachies).

    0 讨论(0)
  • 2020-11-22 01:55

    The problem with systemPath is that the dependencies' jars won't get distributed along your artifacts as transitive dependencies. Try what I've posted here: Is it best to Mavenize your project jar files or put them in WEB-INF/lib?

    Then declare dependencies as usual.

    And please read the footer note.

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