Class DependencyCalculator


  • public class DependencyCalculator
    extends Object
    This class is effectively an unmitigated hack. Any offers to do it 'properly' are gratefully received from someone who can get the guts of Maven to do what want.

    Basically, given our 'uberwar' project, instead of simply merging WEB-INF/lib files, we wish to treat those war files as ordinary dependencies, and to calculate the 'effective' list of jar files that SHOULD be in WEB-INF/lib that way. I.E, if we are including A.WAR and B.WAR, both of which use different versions of X.JAR, then we should calculate what the 'right' version of the X.JAR that we ought to be using.

    This seems very hard to do given the tools provided by maven. There are alternate solutions, such as WAR files producing their code in a JAR as well, but this relies on including BOTH the WAR and the JAR, which is not pretty.

    This class does it a hacky way. For each of the war files in the dependency tree (including the caller), it generates an alternate pom file, which 'pretends' that the WAR file is a POM, and replaces any dependent WARS with equivalent dependencies of type POM with a different classifier. It then invokes maven to resolve that project, which appears to resolve the versions (as would have been done in an all-jar universe).

    A better way would probably be to be able to customise the dependency calculation system, but this seems very bound up in all the project/artifact gubbins.
    • Constructor Summary

      Constructors 
      Constructor Description
      DependencyCalculator​(org.apache.maven.artifact.factory.ArtifactFactory artifactFactory, org.apache.maven.artifact.resolver.ArtifactResolver resolver, org.apache.maven.artifact.repository.ArtifactRepository localRepository, List<org.apache.maven.artifact.repository.ArtifactRepository> remoteRepositories, org.apache.maven.project.MavenProject mavenProject, org.apache.maven.project.MavenProjectBuilder mavenProjectBuilder, org.apache.maven.artifact.installer.ArtifactInstaller installer, org.codehaus.plexus.PlexusContainer container)
      Saves all attributes.
    • Constructor Detail

      • DependencyCalculator

        public DependencyCalculator​(org.apache.maven.artifact.factory.ArtifactFactory artifactFactory,
                                    org.apache.maven.artifact.resolver.ArtifactResolver resolver,
                                    org.apache.maven.artifact.repository.ArtifactRepository localRepository,
                                    List<org.apache.maven.artifact.repository.ArtifactRepository> remoteRepositories,
                                    org.apache.maven.project.MavenProject mavenProject,
                                    org.apache.maven.project.MavenProjectBuilder mavenProjectBuilder,
                                    org.apache.maven.artifact.installer.ArtifactInstaller installer,
                                    org.codehaus.plexus.PlexusContainer container)
        Saves all attributes.
        Parameters:
        artifactFactory - Artifact factory.
        resolver - Artifact resolver.
        localRepository - Artifact repository.
        remoteRepositories - Remote repositories.
        mavenProject - Maven2 project.
        mavenProjectBuilder - Maven2 project builder.
        installer - Artifact installer.
        container - Plexus container.
    • Method Detail

      • execute

        public Set<File> execute()
                          throws Exception
        Execute the dependency calculator.
        Returns:
        List of dependency files.
        Throws:
        Exception - If anything goes wrong.
      • fixupProjectArtifact

        protected void fixupProjectArtifact()
                                     throws Exception
        Fixup the project artifact.
        Throws:
        Exception - If anything goes wrong.
      • fixupRepositoryArtifact

        protected void fixupRepositoryArtifact​(org.apache.maven.artifact.Artifact artifact)
                                        throws Exception
        Fixup an artifact.
        Parameters:
        artifact - Artifact to fixup.
        Throws:
        Exception - If anything goes wrong.
      • fixModelAndSaveInRepository

        protected void fixModelAndSaveInRepository​(org.apache.maven.artifact.Artifact artifact,
                                                   org.apache.maven.model.Model pomFile)
                                            throws Exception
        Fix model and save in repository.
        Parameters:
        artifact - Artifact.
        pomFile - Maven2 model file.
        Throws:
        Exception - If anything goes wrong.