Maven: Access to repositories with custom directory structure

    Welcome all.

    I want to share a small bike with the community - an extension for the maven that allows you to access repositories with a custom structure.
    To begin, I’ll tell you how I got to this. In the process of working on the project, I came up with the idea that JavaScript dependencies like JQuery are not controlled by anything, and when updating, you have to download the libraries manually, which is absolutely not impressive. And so there was a wild desire to find some kind of dependency manager but for javascript. First of all, in my search I came across Bowerbut the need for an extra step during the build process scared off node.js in the dependencies. Then I remembered about CDNs from which you can drag js libraries non-selectively (for example, jquery on Google CDN: http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js). Since the project uses maven for assembly, the logical thought was to set it on these deposits of libraries ... But everything turned out to be not so simple. The fact is that the structure of the CDN file system is different from the standard for maven. After 2 hours of searching for a solution, I could not find a solution on the Internet, and I decided to write my bike. If I have not tired you, then I ask for a cat.
    In the process of finding ready-made solutions, it was noticed that you can write an extension for maven processing a custom type of repository. True, despite the fact that everywhere it was written what can be done, how to do it was not written anywhere. Only once flashed the fact that the RepositoryConnectorFactory interface serves for this purpose. In haste, a simple class was implemented that implements this interface:
    @Component(role = RepositoryConnectorFactory.class, hint = "custom")
    public class CustomRepositoryConnectorFactory implements RepositoryConnectorFactory, Service {
            @Override
            public RepositoryConnector newInstance(RepositorySystemSession session,
                    RemoteRepository repository) throws NoRepositoryConnectorException {
                    System.out.println("CustomRepositoryConnectorFactory.newInstance()");
                    return null;
            }
            @Override
            public int getPriority() {
                    return 1;
            }
            @Override
            public void initService(ServiceLocator locator) {
            }
    }
    
    However, after the extension was connected to the project, a miracle did not happen - the extension was not called, and the Maven continued to swear at the unsupported type of repository. As it turned out later, for the extension to work correctly, you need to generate a component description in the META-INF / plexus / components.xml file. To create which, you can use the plexus-component-metadata plugin, which parses class annotations and creates this magic file.
    org.codehaus.plexusplexus-component-metadata1.5.5generate-metadata
    After enabling the generation of components.xml and installing the plugin in the local repository, everything worked.

    Now I’ll tell you how to use this disgrace. First of all, we connect the repository with the plugin (so far I have laid out in my repository, in the future I will think about how to put it in Maven Central):
    maven-burtsev-nethttp://maven.burtsev.net

    And enable the extension in the build pom.xml section:
    net.burtsev.mavenmaven-custom-repository-layout1.0

    We connect the necessary repositories, for example like this:
    google-cdnhttp://ajax.googleapis.com/ajax/libs/$groupId/$version/$artifactId${classifier(prefix:.)}.$extensioncustom

    The repository URL contains wildcards that will be replaced with the appropriate parameters for the uploaded artifact. I list all supported wildcards:
    • $ groupId
    • $ artifactId
    • $ version
    • $ classifier
    • $ extension

    An alternative syntax is available for parameters whose value may be empty: $ {classifier (prefix :.)}. This is done so as not to duplicate the delimiters in the URL if the parameter is empty.
    After connecting the repository, we connect dependencies for example like this:
    jqueryjquery1.8.2minjs

    To copy the downloaded libraries to the web application folder, use maven-dependency-plugin:
    org.apache.maven.pluginsmaven-dependency-plugincopy-dependenciesgenerate-resourcescopy-dependencies${project.build.directory}/${project.build.finalName}/jsjqueryjs

    Actually everything. The problem is solved - we have achieved the ability to steer JS dependencies using maven.

    The source code of the plugin can be found here: https://bitbucket.org/eburtsev/maven-custom-repository-layout
    The plugin itself can be used directly from my maven repository: http://maven.burtsev.net/
    An example project using the extension described https://bitbucket.org/eburtsev/test-javascript-dependencies

    Also popular now: