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

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

    Using <scope>system</scope> is a terrible idea for reasons explained by others, installing the file manually to your local repository makes the build unreproducible, and using <url>file://${project.basedir}/repo</url> is not a good idea either because (1) that may not be a well-formed file URL (e.g. if the project is checked out in a directory with unusual characters), (2) the result is unusable if this project’s POM is used as a dependency of someone else’s project.

    Assuming you are unwilling to upload the artifact to a public repository, Simeon’s suggestion of a helper module does the job. But there is an easier way now…

    The Recommendation

    Use non-maven-jar-maven-plugin. Does exactly what you were asking for, with none of the drawbacks of the other approaches.

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

    What seems simplest to me is just configure your maven-compiler-plugin to include your custom jars. This example will load any jar files in a lib directory.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>lib/*.jar</include>
                    </includes>
                </configuration>
            </plugin>
    
    0 讨论(0)
  • 2020-11-22 02:02

    A quick&dirty batch solution (based on Alex's answer):

    libs.bat

    @ECHO OFF
    FOR %%I IN (*.jar) DO (
    echo ^<dependency^>
    echo ^<groupId^>local.dummy^</groupId^>
    echo ^<artifactId^>%%I^</artifactId^>
    echo ^<version^>0.0.1^</version^>
    echo ^<scope^>system^</scope^>
    echo ^<systemPath^>${project.basedir}/lib/%%I^</systemPath^>
    echo ^</dependency^>
    )
    

    Execute it like this: libs.bat > libs.txt. Then open libs.txt and copy its content as dependencies.

    In my case, I only needed the libraries to compile my code, and this solution was the best for that purpose.

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

    You may create local repository on your project

    For example if you have libs folder in project structure

    • In libs folder you should create directory structure like: /groupId/artifactId/version/artifactId-version.jar

    • In your pom.xml you should register repository

      <repository>
          <id>ProjectRepo</id>
          <name>ProjectRepo</name>
          <url>file://${project.basedir}/libs</url>
      </repository>
      
    • and add dependency as usual

      <dependency>
          <groupId>groupId</groupId>
          <artifactId>artifactId</artifactId>
          <version>version</version>
      </dependency>
      

    That is all.

    For detailed information: How to add external libraries in Maven

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

    To install the 3rd party jar which is not in maven repository use maven-install-plugin.

    Below are steps:

    1. Download the jar file manually from the source (website)
    2. Create a folder and place your jar file in it
    3. Run the below command to install the 3rd party jar in your local maven repository

    mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=

    Below is the e.g one I used it for simonsite log4j

    mvn install:install-file -Dfile=/Users/athanka/git/MyProject/repo/log4j-rolling-appender.jar -DgroupId=uk.org.simonsite -DartifactId=log4j-rolling-appender -Dversion=20150607-2059 -Dpackaging=jar

    1. In the pom.xml include the dependency as below

        <dependency> 
              <groupId>uk.org.simonsite</groupId>
              <artifactId>log4j-rolling-appender</artifactId>
              <version>20150607-2059</version> 
        </dependency>
      
    2. Run the mvn clean install command to create your packaging

    Below is the reference link:

    https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

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

    This is how we add or install a local jar

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>iamajar</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/iamajar.jar</systemPath>
        </dependency>
    

    i gave some default groupId and artifactId because they are mandatory :)

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