com.smartdoc.util.MojoUtils Maven / Gradle / Ivy
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2019-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.smartdoc.util;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.power.common.util.FileUtil;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiDataDictionary;
import com.power.doc.model.ApiErrorCodeDictionary;
import com.power.doc.model.SourceCodePath;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Objects;
/**
* @author xingzi 2019/12/07 21:19
*/
public class MojoUtils {
/**
* Gson Object
*/
public final static Gson GSON = new GsonBuilder().addDeserializationExclusionStrategy(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
return false;
}
@Override
public boolean shouldSkipClass(Class> aClass) {
return false;
}
}).create();
/**
* Build ApiConfig
*
* @param configFile config file
* @param projectName project name
* @param project Maven project object
* @param log maven plugin log
* @return com.power.doc.model.ApiConfig
* @throws MojoExecutionException MojoExecutionException
*/
public static ApiConfig buildConfig(File configFile, String projectName, MavenProject project, Log log) throws MojoExecutionException {
try {
ClassLoader classLoader = ClassLoaderUtil.getRuntimeClassLoader(project);
String data = FileUtil.getFileContent(new FileInputStream(configFile));
ApiConfig apiConfig = GSON.fromJson(data, ApiConfig.class);
List apiDataDictionaries = apiConfig.getDataDictionaries();
List apiErrorCodes = apiConfig.getErrorCodeDictionaries();
if (apiErrorCodes != null) {
apiErrorCodes.forEach(
apiErrorCode -> {
String className = apiErrorCode.getEnumClassName();
apiErrorCode.setEnumClass(getClassByClassName(className, classLoader));
}
);
}
if (apiDataDictionaries != null) {
apiDataDictionaries.forEach(
apiDataDictionary -> {
String className = apiDataDictionary.getEnumClassName();
apiDataDictionary.setEnumClass(getClassByClassName(className, classLoader));
}
);
}
if (StringUtils.isBlank(apiConfig.getProjectName())) {
apiConfig.setProjectName(projectName);
}
addSourcePaths(project, apiConfig, log);
return apiConfig;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 根据 com.xxx.AClass获取类Class
*
* @param className 类名
* @param classLoader urls
* @return 类类型
*/
public static Class getClassByClassName(String className, ClassLoader classLoader) {
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
private static void addSourcePaths(MavenProject project, ApiConfig apiConfig, Log log) {
List sourceRoots = project.getCompileSourceRoots();
sourceRoots.forEach(s -> apiConfig.setSourceCodePaths(SourceCodePath.path().setPath(s)));
if (project.hasParent()) {
MavenProject mavenProject = project.getParent();
if (null != mavenProject) {
// log.info("--- parent project name is [" + mavenProject.getName() + "]");
File file = mavenProject.getBasedir();
if (!Objects.isNull(file)) {
// log.info("--- parent project basedir is " + file.getPath());
apiConfig.setSourceCodePaths(SourceCodePath.path().setPath(file.getPath()));
// log.info("--- smart-doc-maven-plugin loaded resource from " + file.getPath());
}
}
}
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy