0

我刚刚在我的项目中添加了一个自定义谓词类,而 Maven 构建刚刚因这个错误而中断。我需要添加更多依赖项吗?

[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.21.0:scr (generate-scr-descriptor) on project data-providers: Execution generate-scr-descriptor of goal org.apache.felix:maven-scr-plugin:1.21.0:scr failed: An API incompatibility was encountered while executing org.apache.felix:maven-scr-plugin:1.21.0:scr: java.lang.VerifyError: Constructor must call super() or this() before return
[ERROR] Exception Details:
[ERROR]   Location:
[ERROR]     com/day/cq/search/eval/AbstractPredicateEvaluator.<init>()V @1: return
[ERROR]   Reason:
[ERROR]     Error exists in the bytecode
[ERROR]   Bytecode:
[ERROR]     0x0000000: 2ab1
[ERROR]
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.apache.felix:maven-scr-plugin:1.21.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/rmahna/.m2/repository/org/apache/felix/maven-scr-plugin/1.21.0/maven-scr-plugin-1.21.0.jar
[ERROR] urls[1] = file:/C:/Users/rmahna/.m2/repository/org/apache/maven/maven-archiver/2.2/maven-archiver-2.2.jar
[ERROR] urls[2] = file:/C:/Users/rmahna/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[ERROR] urls[3] = file:/C:/Users/rmahna/.m2/repository/org/codehaus/plexus/plexus-archiver/1.0-alpha-7/plexus-archiver-1.0-alpha-7.jar
[ERROR] urls[4] = file:/C:/Users/rmahna/.m2/repository/org/apache/felix/org.apache.felix.scr.generator/1.13.0/org.apache.felix.scr.generator-1.13.0.jar
[ERROR] urls[5] = file:/C:/Users/rmahna/.m2/repository/org/ow2/asm/asm-all/5.0.2/asm-all-5.0.2.jar
[ERROR] urls[6] = file:/C:/Users/rmahna/.m2/repository/org/osgi/org.osgi.core/4.2.0/org.osgi.core-4.2.0.jar
[ERROR] urls[7] = file:/C:/Users/rmahna/.m2/repository/org/osgi/org.osgi.compendium/4.2.0/org.osgi.compendium-4.2.0.jar
[ERROR] urls[8] = file:/C:/Users/rmahna/.m2/repository/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar
[ERROR] urls[9] = file:/C:/Users/rmahna/.m2/repository/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[project>com.kpmg.cmi.bundles:data-providers:2.0.4, parent: ClassRealm[maven.api, parent: null]]]
[ERROR]
[ERROR] -----------------------------------------------------

我的示例 Java 类如下。我已经删除了业务逻辑,但即使是这个确切的文件也会导致错误。

package my.project.custom.predicates;

import java.util.Comparator;
import javax.jcr.query.Row;
import org.apache.felix.scr.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;

@Component(metatype =false,  factory="com.day.cq.search.eval.PredicateEvaluator/mypredicate_eval")
public class MyCustomPredicate extends AbstractPredicateEvaluator {
    private static final Logger log = LoggerFactory.getLogger(MyCustomPredicate.class);
    public static final String MY_PROPERTY = "my_property";

    @Override
    public boolean includes(Predicate predicate, Row row, EvaluationContext context) {
        if(predicate.hasNonEmptyValue(MY_PROPERTY)){
            return true;
        }
        return super.includes(predicate, row, context);
    }

    @Override
    public String getXPathExpression(Predicate predicate, EvaluationContext context) {

    if(!predicate.hasNonEmptyValue(MY_PROPERTY)){
            return null;
    }
        /**
        * Some generic string comparisons and processing
        */
    return super.getXPathExpression(predicate, context);
    }

    @Override
    public Comparator<Row> getOrderByComparator(final Predicate predicate, final EvaluationContext context) {
       return new Comparator<Row>() {
            public int compare(Row r1, Row r2) {
                    String[] property1;
                    String[] property2;
    try {
        log.info("inside try method");
        //lots of string comparisons

    } catch (Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
    }
                return 1;
            }   
        };
    }
}
4

1 回答 1

0

我看到您正在使用 maven-scr-plugin。这个插件已经完全被 OSGi 规范的最后两个版本的标准注释所取代,现在处于维护模式。我的建议是改用标准注释。标准注解在使用上非常相似,将通过 bnd-maven-plugin 或 maven-bundle-plugin 等工具进行处理。您几乎可以肯定已经在使用这些插件之一 - 这两个插件的当前版本(在撰写本文时)都是 3.5.0。如果您需要处理 Java 8 或 Java 9 字节码,我建议您使用此版本。

生成的文件将如下所示:

package my.project.custom.predicates;

import java.util.Comparator;
import javax.jcr.query.Row;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.search.Predicate;
import com.day.cq.search.eval.AbstractPredicateEvaluator;
import com.day.cq.search.eval.EvaluationContext;

@Component
public class MyCustomPredicate extends AbstractPredicateEvaluator {
    // Your implementation in here
}
于 2018-05-04T16:14:14.367 回答