Maven:未指定来源...但是为什么呢?

Maven: Source Not Specified... But why?

好的,这是我的第一个 Maven 项目。这是我需要完成的最后一件事,但它一直指向我。

java.lang.IllegalStateException: source not specified
    at org.eclipse.jdt.core.dom.ASTParser.createAST (ASTParser.java:830)
    at com.javaAST.ASTParse.parse (ASTParse.java:25)
    at com.javaAST.Main.main (Main.java:59)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:567)
    at org.codehaus.mojo.exec.ExecJavaMojo.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:830)

最后是从这个页面

package com.javaAST;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

public class ASTParse
{
    public static FileList fileList = new FileList();
    public static ASTParser parser = ASTParser.newParser(AST.JLS3);

    public static void parse(List<String> results) throws IOException
    {

        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

        for (String result : results)
        {
            //Create output file
            String addExtension = result.toLowerCase() + ".ast";
            System.out.println("Creating File: " + addExtension);
            File output = new File(addExtension);
            output.createNewFile();
            FileWriter fw = new FileWriter(output);


            //Begin ASTParser
            parser.setSource(result.toCharArray());
            parser.setKind(ASTParser.K_COMPILATION_UNIT);

            cu.accept(new ASTVisitor() {

                Set<String> names = new HashSet<>();

                public boolean visit(VariableDeclarationFragment node) {
                    SimpleName name = node.getName();
                    this.names.add(name.getIdentifier());
                    try
                    {
                        fw.write("Declaration of '" + name + "' at line" + cu.getLineNumber(name.getStartPosition()) + "\n");
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    return false; // do not continue to avoid usage info
                }

                public boolean visit(SimpleName node) {
                    if (this.names.contains(node.getIdentifier())) {
                        try
                        {
                            fw.write("Usage of '" + node + "' at line " + cu.getLineNumber(node.getStartPosition()) + "\n");
                        } catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                    }
                    return true;
                }
            });
        }
    }
}

并且 pom.xml 确实包含

        <dependency>
            <groupId>org.eclipse.jdt</groupId>
            <artifactId>org.eclipse.jdt.core</artifactId>
            <version>3.20.0</version>
        </dependency>

,但我一直收到此错误。现在 .jar 正在创建这个文件,ASTParser.classASTParser.class 我还没有完全理解,但我认为这不足以破坏它。任何有经验的人的指导?

问题不在于您的 maven 设置。您的代码使用 AST 解析器的方式有误。您在第 25 行收到 IllegalStateException

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

根据 API specs for the Eclipse AST parser,此异常在 createAST 上抛出,当时:

IllegalStateException - if the settings provided are insufficient, contradictory, or otherwise unsupported

异常消息中有进一步的线索:'source not specified'。您需要设置一个来源,大概是通过使用 parser.setSource()before 调用 createAST.