Maven: keeping dependent jars in project version control

后端 未结 5 1922
猫巷女王i
猫巷女王i 2021-01-02 01:37

So, I have a war project with several dependent jars that are not available in any repository. Up until recently, I\'d been keeping them in src/main/webapp/WEB-INF/lib

相关标签:
5条回答
  • 2021-01-02 01:44

    If setting up Nexus or just a simple fileserver repo as suggested by Jan is really too much trouble, here are some other options:

    • Simply include the JARs and a script that'll install them all in a directory of your choosing in version control. No need for the Maven structure to sanction it.
    • Use your version control system as a host for the repository, either on a branch by itself in your main project or as a separate project. Let any backup, security, etc. you already have on version control apply to the repository without including the jars in the set of files that are actually being checked out and committed.
    • Include the jars in a folder that acts as a mini-repository that is checked out with the rest of the code. Have pom.xml point to that folder as a repository it uses. I'm not 100% sure this is possible, but it seems likely that it is.
    0 讨论(0)
  • 2021-01-02 01:44

    At my company we are using Archiva, it is by far the simplest Repository Manager to set up and maintain. Especially if you go with the stand-alone based version. The each developer just sets up a profile in their ~/.m2/settings.xml file to point to the internal repository. If this is too much trouble, just put the internal repository in the <repositories/> in the pom.xml directly, but that is really bad practice. If the repository url ever moves, you have to update all your projects pom.xml files. Where as using settings.xml puts that burden on the developer to update their local configuration.

    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
        <profiles>
            <profile>
                <id>internal</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <repositories>
                    <repository>
                        <id>mycompany.internal</id>
                        <name>Internal Release Repository</name>
                        <url>http://maven.mycompany.com/repository/internal/</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>false</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>mycompany.snapshots</id>
                        <name>Internal Snapshot Repository</name>
                        <url>http://maven.mycompany.com/repository/snapshots/</url>
                        <releases>
                            <enabled>false</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                </repositories>
            </profile>
        </profiles>
    
    
        <servers>
            <server>
                <id>internal</id>
                <username>guest</username>
            </server>
            <server>
                <id>snapshots</id>
                <username>guest</username>
            </server>
        </servers>
    
    </settings>
    

    If setting up a Repository Manager is too much trouble, I think you need to reconsider the alternative of manually adding stuff to your local repository by hand, which is very error prone and time consuming. I run an Archiva instance for my personal development just because it is so easy to add the release plug in and manage versions without having to remember all the arcane -D options required to add stuff to your local repository on every machine. Copying the ~/.m2/settings.xml file is super easy and if it works on one machine it works on all of them.

    Here is what you add to your pom.xml to enable automatically doing releases and pushing artifacts to a repository, in my case it is Archiva.

    <distributionManagement>
       <repository>
           <id>internal</id>
           <name>Internal Archiva Repository</name>
           <url>http://maven.mycompany.com/repository/internal/</url>
           <layout>default</layout>
           <uniqueVersion>false</uniqueVersion>
       </repository>
       <snapshotRepository>
           <id>snapshots</id>
           <name>Internal Archiva Repository</name>
           <url>http://maven.mycompany.com/repository/snapshots/</url>
           <layout>default</layout>
           <uniqueVersion>false</uniqueVersion>
       </snapshotRepository>
    </distributionManagement>
    

    Then all you do is mvn clean release:prepare to automatically update your pom.xml version check in, tag and optionally branch the release and package all the artifacts, and then mvn release:perform to push the artifacts to the remote repository and check in the newly versioned pom.xml and you are ready for the next version to start development.

    Snapshots got to snaphots and Releases go to internal automatically with the release plug in. You do have to configure the scm plug in as well, but that is just a few lines of configuration that you only have to touch once as well.

    This is what it looks like for git, we are using Gitorious as our git Repository Manager

    <scm>
        <connection>scm:git:git://gitorious.mycompany.com:myproject/myproject.git</connection>
        <developerConnection>scm:git:ssh://git@gitorious.mycompany.com/myproject/myproject.git</developerConnection>
        <url>http://gitorious.mycompany.com/myproject/myproject</url>
    </scm>
    

    An old desktop computer running Linux can handle repository duties like this for a development team without having to involve procurement and IT.

    0 讨论(0)
  • 2021-01-02 01:49

    I think the best solution in your case would be to setup an internal repository. There is no need to setup whole Nexus at first. Any simple webserver will be sufficient or shared folder somewhere.

    Check the Maven DOC on howto set up an internal repository.

    0 讨论(0)
  • 2021-01-02 01:51

    I have not used this in real life, but it seems to be working: simply stick a repository layout inside your source tree (using install:install-file, I suppose) and and check it in. Use a entry to point to that repo. pom like so:

    <?xml version="1.0" encoding="utf-8"?>
    <project>
      <modelVersion>4.0.0</modelVersion>
      <name>Depending project</name>
    
      <groupId>com.example</groupId>
      <artifactId>dependent</artifactId>
      <version>0.9</version>
      <packaging>jar</packaging>
    
      <dependencies>
        <dependency>
          <groupId>com.example</groupId>
          <artifactId>dependency</artifactId>
          <version>0.9.3</version>
          <type>jar</type>
        </dependency>
      </dependencies>
    
      <repositories>
    
        <repository>
          <id>project-specific-deps</id>
          <name>project-specific-deps</name>
          <url>file:///${basedir}/repo</url>
        </repository>
    
      </repositories>
    
    </project>
    

    Example tree like so:

    .
    |-- pom.xml
    |-- repo
    |   `-- com
    |       `-- example
    |           `--dependency
    |               |-- 0.9.3
    |               |   |-- dependency-0.9.3.jar
    |               |   |-- dependency-0.9.3.jar.md5
    |               |   |-- dependency-0.9.3.jar.sha1
    |               |   |-- dependency-0.9.3.pom
    |               |   |-- dependency-0.9.3.pom.md5
    |               |   `-- dependency-0.9.3.pom.sha1
    |               |-- maven-metadata.xml
    |               |-- maven-metadata.xml.md5
    |               `-- maven-metadata.xml.sha1
    |-- src
    |   `-- main
    |       `-- java
    |           `-- com
    |               `-- example
    |                   `-- dependent
    |                       `-- Foobar.java
    

    How does that sound?

    For completeness:

    $ mvn -version
    Apache Maven 2.2.1 (rdebian-4)
    Java version: 1.6.0_20
    Java home: /usr/lib/jvm/java-6-sun-1.6.0.20/jre
    Default locale: sv_SE, platform encoding: ISO-8859-1
    OS name: "linux" version: "2.6.32-3-686" arch: "i386" Family: "unix"
    
    0 讨论(0)
  • 2021-01-02 01:59

    There are lots of good answers here already, and you should strongly consider setting up a repository manager such as Archiva for your group as it'll provide other benefits both now and over time.

    However, to directly answer your question, here is a pattern for storing dependencies in version control with Maven: http://brettporter.wordpress.com/2009/06/10/a-maven-friendly-pattern-for-storing-dependencies-in-version-control/

    Please keep in mind the caveat that it limits the project to being used self-contained itself - if you start wanting to share it as a dependency for other Maven projects you'll still need a repository set up.

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