Maven: add a dependency to a jar by relative path

后端 未结 9 1009
半阙折子戏
半阙折子戏 2020-11-21 23:21

I have a proprietary jar that I want to add to my pom as a dependency.

But I don\'t want to add it to a repository. The reason is that I want my usual maven commands

相关标签:
9条回答
  • 2020-11-22 00:21

    This is working for me: Let's say I have this dependency

    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>my-library</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/my-library.jar</systemPath>
    </dependency>
    

    Then, add the class-path for your system dependency manually like this

    <Class-Path>libs/my-library-1.0.jar</Class-Path>
    

    Full config:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifestEntries>
                    <Build-Jdk>${jdk.version}</Build-Jdk>
                    <Implementation-Title>${project.name}</Implementation-Title>
                    <Implementation-Version>${project.version}</Implementation-Version>
                    <Specification-Title>${project.name} Library</Specification-Title>
                    <Specification-Version>${project.version}</Specification-Version>
                    <Class-Path>libs/my-library-1.0.jar</Class-Path>
                </manifestEntries>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.company.app.MainClass</mainClass>
                    <classpathPrefix>libs/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/libs/</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-11-22 00:21

    Basically, add this to the pom.xml:

    ...
    
    <repositories>
       <repository>
           <id>lib_id</id>
           <url>file://${project.basedir}/lib</url>
       </repository>
    </repositories>
    
    ...
    
    <dependencies>
      ...
      <dependency>
          <groupId>com.mylibrary</groupId>
          <artifactId>mylibraryname</artifactId>
          <version>1.0.0</version>
      </dependency>
      ...
    </dependencies>
    
    0 讨论(0)
  • 2020-11-22 00:21

    we switched to gradle and this works much better in gradle ;). we just specify a folder we can drop jars into for temporary situations like that. We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven). This is just one more dependency we define.

    so basically now we can just drop any jar we want into our lib dir for temporary testing if it is not a in maven repository somewhere.

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