代码之家  ›  专栏  ›  技术社区  ›  Jeff Storey

maven jdepend失败构建(含周期)

  •  3
  • Jeff Storey  · 技术社区  · 14 年前

    谢谢, 杰夫

    3 回复  |  直到 14 年前
        1
  •  4
  •   Daniel Seidewitz    13 年前

    您可以为maven enforcer插件编写自己的规则,如中所述

    http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html

    我就是这样做的。

    NoPackageCyclesRule.java包

    package org.apache.maven.enforcer.rule;
    
    import java.io.File;
    import java.io.IOException;
    
    import jdepend.framework.JDepend;
    
    import org.apache.maven.enforcer.rule.api.EnforcerRule;
    import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
    import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
    import org.apache.maven.plugin.logging.Log;
    import org.apache.maven.project.MavenProject;
    import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
    
    public class NoPackageCyclesRule implements EnforcerRule
    {
    
        public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException
        {
            Log log = helper.getLog();
    
            try
            {
                MavenProject project = (MavenProject) helper.evaluate("${project}");
                File targetDir = new File((String) helper.evaluate("${project.build.directory}"));
                File classesDir = new File(targetDir, "classes");
    
                if (project.getPackaging().equalsIgnoreCase("jar") && classesDir.exists())
                {
                    JDepend jdepend = new JDepend();
                    jdepend.addDirectory(classesDir.getAbsolutePath());
                    jdepend.analyze();
    
                    if (jdepend.containsCycles())
                    {
                        throw new EnforcerRuleException("There are package cycles");
                    }
                }
                else
                {
                    log.warn("Skipping jdepend analysis as " + classesDir + " does not exist.");
                }
            }
            catch (ExpressionEvaluationException e)
            {
                throw new EnforcerRuleException("Unable to lookup an expression "
                        + e.getLocalizedMessage(), e);
            }
            catch (IOException e)
            {
                throw new EnforcerRuleException("Unable to access target directory "
                        + e.getLocalizedMessage(), e);
            }
        }
    
        public String getCacheId()
        {
            return "";
        }
    
        public boolean isCacheable()
        {
            return false;
        }
    
        public boolean isResultValid(EnforcerRule arg0)
        {
            return false;
        }
    }
    

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                          http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>custom-rule</groupId>
      <artifactId>no-package-cycles-rule</artifactId>
      <version>1.0</version>
    
      <properties>
        <api.version>1.0</api.version>
        <maven.version>2.2.1</maven.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.enforcer</groupId>
          <artifactId>enforcer-api</artifactId>
          <version>${api.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-project</artifactId>
          <version>${maven.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
          <version>${maven.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-artifact</artifactId>
          <version>${maven.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-plugin-api</artifactId>
          <version>${maven.version}</version>
        </dependency>
        <dependency>
          <groupId>org.codehaus.plexus</groupId>
          <artifactId>plexus-container-default</artifactId>
          <version>1.5.5</version>
        </dependency>
        <dependency>
          <groupId>jdepend</groupId>
          <artifactId>jdepend</artifactId>
          <version>2.9.1</version>
        </dependency>
      </dependencies>
    
    </project>
    

      <plugin>
        <artifactId>maven-enforcer-plugin</artifactId>
        <dependencies>
          <dependency>
            <groupId>custom-rule</groupId>
            <artifactId>no-package-cycles-rule</artifactId>
            <version>1.0</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>enforce-no-package-cycles</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <phase>verify</phase> <!-- use a phase after compile! -->
            <configuration>
              <rules>
                <NoPackageCyclesRule
                  implementation="org.apache.maven.enforcer.rule.NoPackageCyclesRule" />
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    
        2
  •  5
  •   Ben Romberg    10 年前

    基于公认的答案,我改进了性能和日志输出,并在Maven Central上发布了它:

    https://github.com/andrena/no-package-cycles-enforcer-rule

    (我会对接受的答案发表评论,但还没有足够的代表。)

        3
  •  1
  •   Pascal Thivent    14 年前

    在我看来 JDepend Maven Plugin 应该用于生成报告,它不允许在违反特定规则的情况下使构建失败。

    推荐文章