Greetings to all.
I want to share with the community a small bicycle - an extension for mavena that allows access to repositories with a custom structure.
To begin, I will tell you how I got to this. In the process of working on a project, the idea came about the fact that JavaScript dependencies like jQuery are not controlled by anything, and during the update you have to download the libraries manually, which is not at all 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 stumbled upon
Bower but the need to introduce an additional step in the build process scared off as node.js dependencies. Then I remembered the CDN from which you can unambiguously pull the js-library (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, it was a logical thought to incite 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 on the Internet, no one was found, and I decided to write my bicycle. If I have not tired you then I ask under the cat.
In the process of finding ready-made solutions, it was noticed that you can write an extension for the maven processing custom repository type. True, despite the fact that everywhere it was written what can be done, it was not written anywhere else. Only once flashed what the RepositoryConnectorFactory interface serves for this purpose. In a hurry, 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 connecting the extension to the project, the miracle did not happen - the extension was not called, and maven continued to swear at the unsupported repository type. As it turned out, for the extension to work properly, you need to generate a component description in the META-INF / plexus / components.xml file. To create which, you can use the plugin plexus-component-metadata, which parses the annotations to the classes and creates this magic file.
<plugin> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-metadata</artifactId> <version>1.5.5</version> <executions> <execution> <goals> <goal>generate-metadata</goal> </goals> </execution> </executions> </plugin>
After turning on the generation of components.xml and installing the plugin in the local repository, everything worked.
Now I will tell how to use this disgrace. First of all, we connect the repository with the plugin (for the time being I have laid out in my repository, in the future I will think how to put it in Maven Central):
<pluginRepositories> <pluginRepository> <id>maven-burtsev-net</id> <url>http://maven.burtsev.net</url> </pluginRepository> </pluginRepositories>
And we include the extension in the build pom.xml section:
<build> <extensions> <extension> <groupId>net.burtsev.maven</groupId> <artifactId>maven-custom-repository-layout</artifactId> <version>1.0</version> </extension> </extensions> </build>
We connect necessary repositories, for example so:
<repository> <id>google-cdn</id> <url>http://ajax.googleapis.com/ajax/libs/$groupId/$version/$artifactId${classifier(prefix:.)}.$extension</url> <layout>custom</layout> </repository>
Wildcards are specified in the repository URL, which will be replaced with the corresponding parameters for the injected artifact. Here is a list of all supported wildcard characters:
- $ groupId
- $ artifactId
- $ version
- $ classifier
- $ extension
For parameters whose value may be empty, an alternative syntax is available: $ {classifier (prefix :.)}. This is done so as not to duplicate the delimiters in the URL if the parameter value is empty.
After connecting the repository, we add dependencies like this:
<dependencies> <dependency> <groupId>jquery</groupId> <artifactId>jquery</artifactId> <version>1.8.2</version> <classifier>min</classifier> <type>js</type> </dependency> </dependencies>
To copy the loaded libraries to the web application folder, use the maven-dependency-plugin:
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>generate-resources</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.build.finalName}/js</outputDirectory> <includeArtifactIds>jquery</includeArtifactIds> <includeTypes>js</includeTypes> </configuration> </execution> </executions> </plugin> </plugins>
Actually everything. Problem solved - we managed to steer JS dependencies using maven.
')
The source code of the plugin can be found here:
https://bitbucket.org/eburtsev/maven-custom-repository-layoutThe plugin itself can be used directly from my maven repository:
http://maven.burtsev.net/Sample project using the described extension
https://bitbucket.org/eburtsev/test-javascript-dependencies