Search in sources :

Example 1 with ProjectParseException

use of meghanada.project.ProjectParseException in project meghanada-server by mopemope.

the class GradleProject method parseProject.

@Override
public Project parseProject() throws ProjectParseException {
    final ProjectConnection connection = getProjectConnection();
    log.info("loading gradle project:{}", new File(this.projectRoot, Project.GRADLE_PROJECT_FILE));
    try {
        BuildEnvironment env = connection.getModel(BuildEnvironment.class);
        String version = env.getGradle().getGradleVersion();
        if (isNull(version)) {
            version = GradleVersion.current().getVersion();
        }
        if (nonNull(version)) {
            this.gradleVersion = new ComparableVersion(version);
        }
        final IdeaProject ideaProject = debugTimeItF("get idea project model elapsed={}", () -> connection.getModel(IdeaProject.class));
        this.setCompileTarget(ideaProject);
        log.trace("load root project path:{}", this.rootProject);
        final DomainObjectSet<? extends IdeaModule> modules = ideaProject.getModules();
        final List<? extends IdeaModule> mainModules = modules.parallelStream().filter(ideaModule -> {
            final org.gradle.tooling.model.GradleProject gradleProject = ideaModule.getGradleProject();
            final File moduleProjectRoot = gradleProject.getProjectDirectory();
            final String name = ideaModule.getName();
            log.trace("find sub-module name {} path {} ", name, moduleProjectRoot);
            this.allModules.putIfAbsent(name, moduleProjectRoot);
            return moduleProjectRoot.equals(this.getProjectRoot());
        }).collect(Collectors.toList());
        mainModules.forEach(wrapIOConsumer(this::parseIdeaModule));
        // set default output
        if (isNull(super.output)) {
            String build = Joiner.on(File.separator).join(this.projectRoot, "build", "classes", "main");
            if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
                build = Joiner.on(File.separator).join(this.projectRoot, "build", "classes", "java", "main");
            }
            super.output = this.normalize(build);
        }
        if (isNull(super.testOutput)) {
            String build = Joiner.on(File.separator).join(this.projectRoot, "build", "classes", "test");
            if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
                build = Joiner.on(File.separator).join(this.projectRoot, "build", "classes", "java", "test");
            }
            super.testOutput = this.normalize(build);
        }
        return this;
    } catch (Exception e) {
        throw new ProjectParseException(e);
    } finally {
        connection.close();
    }
}
Also used : Config.debugTimeItF(meghanada.config.Config.debugTimeItF) IdeaDependency(org.gradle.tooling.model.idea.IdeaDependency) ProjectParseException(meghanada.project.ProjectParseException) IdeaJavaLanguageSettings(org.gradle.tooling.model.idea.IdeaJavaLanguageSettings) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) StringUtils(org.apache.commons.lang3.StringUtils) PipedInputStream(java.io.PipedInputStream) Map(java.util.Map) GradleConnector(org.gradle.tooling.GradleConnector) Objects.isNull(java.util.Objects.isNull) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) Splitter(com.google.common.base.Splitter) GradleVersion(org.gradle.util.GradleVersion) BuildEnvironment(org.gradle.tooling.model.build.BuildEnvironment) BuildLauncher(org.gradle.tooling.BuildLauncher) CompileResult(meghanada.analyze.CompileResult) AndroidProject(com.android.builder.model.AndroidProject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) ResultHandler(org.gradle.tooling.ResultHandler) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClassNameUtils(meghanada.utils.ClassNameUtils) IdeaContentRoot(org.gradle.tooling.model.idea.IdeaContentRoot) IdeaSingleEntryLibraryDependency(org.gradle.tooling.model.idea.IdeaSingleEntryLibraryDependency) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) Project(meghanada.project.Project) Objects.nonNull(java.util.Objects.nonNull) ProjectDependency(meghanada.project.ProjectDependency) Config(meghanada.config.Config) Joiner(com.google.common.base.Joiner) HashMap(java.util.HashMap) GradleConnectionException(org.gradle.tooling.GradleConnectionException) IdeaSourceDirectory(org.gradle.tooling.model.idea.IdeaSourceDirectory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IdeaModule(org.gradle.tooling.model.idea.IdeaModule) Files(com.google.common.io.Files) ConfigFactory(com.typesafe.config.ConfigFactory) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) IdeaModuleDependency(org.gradle.tooling.model.idea.IdeaModuleDependency) DomainObjectSet(org.gradle.tooling.model.DomainObjectSet) IOException(java.io.IOException) PipedOutputStream(java.io.PipedOutputStream) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) IdeaProject(org.gradle.tooling.model.idea.IdeaProject) GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion) ProjectConnection(org.gradle.tooling.ProjectConnection) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) IdeaProject(org.gradle.tooling.model.idea.IdeaProject) ProjectConnection(org.gradle.tooling.ProjectConnection) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) ProjectParseException(meghanada.project.ProjectParseException) GradleConnectionException(org.gradle.tooling.GradleConnectionException) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) IOException(java.io.IOException) ProjectParseException(meghanada.project.ProjectParseException) BuildEnvironment(org.gradle.tooling.model.build.BuildEnvironment) File(java.io.File)

Example 2 with ProjectParseException

use of meghanada.project.ProjectParseException in project meghanada-server by mopemope.

the class POMParser method parsePom.

POMInfo parsePom(File pom) throws ProjectParseException {
    try {
        if (!pom.isAbsolute()) {
            pom = pom.getCanonicalFile();
        }
        ModelBuildingRequest req = new DefaultModelBuildingRequest();
        req.setPomFile(pom);
        req.setSystemProperties(System.getProperties());
        req.setModelResolver(new LocalModelResolver(this.projectRoot));
        req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        req.setProcessPlugins(true);
        DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
        DefaultModelBuilder builder = factory.newInstance();
        ModelBuildingResult result = builder.build(req);
        Model mavenModel = result.getEffectiveModel();
        POMInfo pomInfo = new POMInfo(pom.getParent());
        String groupId = mavenModel.getGroupId();
        if (nonNull(groupId)) {
            pomInfo.groupId = groupId;
        }
        String artifactId = mavenModel.getArtifactId();
        if (nonNull(artifactId)) {
            pomInfo.artifactId = artifactId;
        }
        String version = mavenModel.getVersion();
        if (nonNull(version)) {
            pomInfo.version = version;
        }
        Properties modelProperties = mavenModel.getProperties();
        if (nonNull(modelProperties)) {
            if (nonNull(groupId)) {
                modelProperties.put("project.groupId", groupId);
            }
            if (nonNull(artifactId)) {
                modelProperties.put("project.artifactId", artifactId);
            }
            if (nonNull(version)) {
                modelProperties.put("project.version", version);
            }
            for (String key : modelProperties.stringPropertyNames()) {
                String value = modelProperties.getProperty(key);
                if (nonNull(value)) {
                    pomInfo.properties.setProperty(key, value);
                }
            }
            POMParser.replacePOMProperties(pomInfo);
        }
        Build build = mavenModel.getBuild();
        this.parseBuild(pomInfo, build);
        return pomInfo;
    } catch (IOException | ModelBuildingException e) {
        throw new ProjectParseException(e);
    }
}
Also used : ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) IOException(java.io.IOException) Properties(java.util.Properties) DefaultModelBuilder(org.apache.maven.model.building.DefaultModelBuilder) DefaultModelBuilderFactory(org.apache.maven.model.building.DefaultModelBuilderFactory) ProjectParseException(meghanada.project.ProjectParseException) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException)

Example 3 with ProjectParseException

use of meghanada.project.ProjectParseException in project meghanada-server by mopemope.

the class MavenProject method parseProject.

@Override
public Project parseProject() throws ProjectParseException {
    try {
        final String mavenPath = Config.load().getMavenPath();
        if (!Strings.isNullOrEmpty(mavenPath)) {
            this.mavenCmd = mavenPath;
        }
        final File logFile = File.createTempFile("meghanada-maven-classpath", ".log");
        logFile.deleteOnExit();
        final String logPath = logFile.getCanonicalPath();
        log.info("running maven. resolve dependencies ...");
        if (this.runMvn(RESOLVE_TASK, SOURCES_TASK, BUILD_CLASSPATH_TASK, String.format("-Dmdep.outputFile=%s", logPath)) != 0) {
            throw new ProjectParseException("Could not resolve dependencies. please try 'mvn dependency:resolve' or 'mvn install'");
        }
        final String cpTxt = Files.asCharSource(logFile, StandardCharsets.UTF_8).readFirstLine();
        if (cpTxt != null && !cpTxt.isEmpty()) {
            for (final String dep : Splitter.on(File.pathSeparator).split(cpTxt)) {
                final File file = new File(dep);
                final String parentPath = file.getParent();
                final String version = MavenProject.getVersion(parentPath);
                final String code = MavenProject.getArtifactCode(parentPath);
                final ProjectDependency.Type type = ProjectDependency.getFileType(file);
                final ProjectDependency dependency = new ProjectDependency(code, "COMPILE", version, file, type);
                super.dependencies.add(dependency);
            }
        }
        final POMParser pomParser = new POMParser(this.projectRoot);
        final POMInfo pomInfo = pomParser.parsePom(this.pomFile);
        {
            super.sources.addAll(pomInfo.getSourceDirectory());
            File src = new File(this.projectRoot, String.join(File.separator, "src", "main", "java"));
            super.sources.add(src);
            super.resources.addAll(pomInfo.getResourceDirectories());
            File resource = new File(this.projectRoot, String.join(File.separator, "src", "main", "resource"));
            super.resources.add(resource);
            File output = pomInfo.getOutputDirectory();
            if (output == null) {
                output = new File(this.projectRoot, String.join(File.separator, "target", "classes"));
            }
            super.output = output;
        }
        {
            super.testSources.addAll(pomInfo.getTestSourceDirectory());
            File src = new File(this.projectRoot, String.join(File.separator, "src", "test", "java"));
            super.testSources.add(src);
            super.testResources.addAll(pomInfo.getTestResourceDirectories());
            File testResource = new File(this.projectRoot, String.join(File.separator, "src", "test", "resource"));
            super.testResources.add(testResource);
            File output = pomInfo.getTestOutputDirectory();
            if (output == null) {
                output = new File(this.projectRoot, String.join(File.separator, "target", "test-classes"));
            }
            super.testOutput = output;
        }
        super.compileSource = pomInfo.compileSource;
        super.compileTarget = pomInfo.compileTarget;
    } catch (IOException | InterruptedException e) {
        throw new ProjectParseException(e);
    }
    return this;
}
Also used : ProjectParseException(meghanada.project.ProjectParseException) IOException(java.io.IOException) ProjectDependency(meghanada.project.ProjectDependency) File(java.io.File)

Aggregations

IOException (java.io.IOException)3 ProjectParseException (meghanada.project.ProjectParseException)3 File (java.io.File)2 ProjectDependency (meghanada.project.ProjectDependency)2 AndroidProject (com.android.builder.model.AndroidProject)1 Joiner (com.google.common.base.Joiner)1 Splitter (com.google.common.base.Splitter)1 Files (com.google.common.io.Files)1 ConfigFactory (com.typesafe.config.ConfigFactory)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects.isNull (java.util.Objects.isNull)1 Objects.nonNull (java.util.Objects.nonNull)1