提交 b803cec0 编写于 作者: Y yuhaowin

修改项目使得可以 preview

上级 6a1383ef
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yuhaowin.tools</groupId>
<artifactId>githook-maven-plugin</artifactId>
<version>1.0.0</version>
<packaging>maven-plugin</packaging>
<name>Git Hook Plugin</name>
<description>Maven plugin to configure and install local git hooks</description>
<artifactId>git-tools</artifactId>
<version>1.0</version>
<name>git-tools</name>
<description>git-tools</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.3.9</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.2.1.201812262042-r</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
<phase>process-classes</phase>
</execution>
<execution>
<id>help-descriptor</id>
<goals>
<goal>helpmojo</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
autoOpen: true
autoOpen: false
apps:
- port: 8501
command: mvn package -Dmaven.test.skip=true
root: ./
name: git-hook-tools
description: git-hook-tools
autoOpen: true
\ No newline at end of file
- root: .
port: 8080
run: mvn package && java -jar target/git-tools-1.0.jar
autoOpen: true
package com.yuhaowin.tools;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
@SpringBootApplication
public class GitHookInstallMojo {
@Mojo(name = "install", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true)
public final class GitHookInstallMojo extends AbstractMojo {
private static final String NEW_LINE = System.lineSeparator();
private static final String SHEBANG = "#!/bin/sh" + NEW_LINE;
private static final List<String> validHooks = Arrays.asList(
"applypatch-msg",
"pre-applypatch",
"post-applypatch",
"pre-commit",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"pre-rebase",
"post-checkout",
"post-merge",
"pre-receive",
"update",
"post-receive",
"post-update",
"pre-auto-gc",
"post-rewrite",
"pre-push");
/**
* The hooks that should be installed. For each map entry, the key must be a
* valid Git hook name (see https://git-scm.com/docs/githooks#_hooks) and the
* value is the script to install
*/
@Parameter(name = "hooks")
private Map<String, String> hooks;
/**
* external git hooks shell script
*/
@Parameter(name = "resource-hooks")
private Map<String, String> resourceHooks;
/**
* Used to validate the .git directory,should appear in the hierarchy of where
* the project is being built
*/
@Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
private String buildDirectory;
/**
* Whether the plugin should be skipped
*/
@Parameter(property = "githook.plugin.skip")
private boolean skip = false;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("Skipping GitHook plugin execution");
return;
}
Path hooksDir = getOrCreateHooksDirectory(buildDirectory);
if (hooksDir == null) {
getLog().info("No .git directory found, skipping plugin execution");
throw new MojoExecutionException(String.format("Not a git repository, could not find a .git/hooks directory anywhere in the hierarchy of %s.", buildDirectory));
}
buildHooks(hooksDir);
buildResourceHooks(hooksDir);
}
private void buildHooks(Path hooksDir) throws MojoExecutionException {
if (hooks == null || hooks.isEmpty()) {
getLog().info("hooks is empty,skip...");
return;
}
for (Map.Entry<String, String> hook : hooks.entrySet()) {
String hookName = hook.getKey();
if (!validHooks.contains(hookName)) {
getLog().error(String.format("`%s` hook is not a valid git-hook name", hookName));
continue;
}
String hookScript = hook.getValue();
String finalScript = (hookScript.startsWith("#!") ? "" : SHEBANG) + hookScript + NEW_LINE;
try {
getLog().info(String.format("Installing %s hook into %s", hookName, hooksDir.toAbsolutePath()));
writeFile(hooksDir.resolve(hookName), finalScript.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new MojoExecutionException("Could not write hook with name: " + hookName, e);
}
}
public static void main(String[] args) {
SpringApplication.run(GitHookInstallMojo.class, args);
}
private void buildResourceHooks(Path hooksDir) throws MojoExecutionException {
if (resourceHooks == null || resourceHooks.isEmpty()) {
getLog().info("resource-hooks is empty,skip...");
return;
}
for (Map.Entry<String, String> hook : resourceHooks.entrySet()) {
String hookName = hook.getKey();
if (!validHooks.contains(hookName)) {
getLog().error(String.format("`%s` hook is not a valid git-hook name", hookName));
continue;
}
Path local = Paths.get("");
Path hookFilePath = Paths.get(hook.getValue());
if (!hookFilePath.toAbsolutePath().startsWith(local.toAbsolutePath())) {
throw new MojoExecutionException("only file inside the project can be used to generate git hooks");
}
try {
getLog().info("Installing " + hookName + " from " + hookFilePath);
String finalScript = Files.lines(hookFilePath).collect(Collectors.joining("\n"));
writeFile(hooksDir.resolve(hookName), finalScript.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new MojoExecutionException("could not access hook resource : " + hookFilePath, e);
}
}
}
private synchronized void writeFile(Path path, byte[] bytes) throws IOException {
File created = Files.write(path, bytes, CREATE, TRUNCATE_EXISTING).toFile();
boolean success = created.setExecutable(true, true)
&& created.setReadable(true, true)
&& created.setWritable(true, true);
if (!success) {
throw new IllegalStateException(String.format("Could not set permissions on created file %s", created.getAbsolutePath()));
}
}
private Path getOrCreateHooksDirectory(String base) {
getLog().debug(String.format("Searching for .git directory starting at %s", base));
File gitMetadataDir = new FileRepositoryBuilder().findGitDir(new File(base)).getGitDir();
if (gitMetadataDir == null) {
return null;
}
Path hooksDir = gitMetadataDir.toPath().resolve("hooks");
if (!hooksDir.toFile().exists()) {
getLog().info(String.format("Creating missing hooks directory at %s", hooksDir.toAbsolutePath()));
try {
Files.createDirectories(hooksDir);
} catch (IOException e) {
getLog().error(e);
return null;
}
}
return hooksDir;
}
}
package com.yuhaowin.tools.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/")
public String index() {
return "git tools success";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册