You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/07/10 03:49:40 UTC

[servicecomb-toolkit] 07/08: SCB-1351 Some issues about using toolkit plugin

This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git

commit 325d2bd335304031ea091f0ca3f3e5eb49b1bb65
Author: MabinGo <bi...@huawei.com>
AuthorDate: Tue Jul 9 18:47:05 2019 +0800

    SCB-1351 Some issues about using toolkit plugin
    
    Remove .class and compile source code to generate .class using the Runtime
    
    Signed-off-by: MabinGo <bi...@huawei.com>
---
 pom.xml                                            |   1 +
 .../toolkit/plugin/GenerateContractsDocMojo.java   |   2 +-
 .../java/plugin/GenerateContractsDocMojoTest.java  |  35 ++++++++++++------
 .../java/plugin/GenerateContractsMojoTest.java     |  14 ++++++--
 .../src/test/java/util/ClassMaker.java             |  33 +++++++++++++++++
 .../contract/swagger.yaml                          |   0
 .../pom-gencontract.xml}                           |  14 ++++++++
 .../pom.xml                                        |  17 ++++++++-
 .../src/main/java/demo/Application.java            |  31 ++++++++++++++++
 .../pom-gencontract.xml}                           |  19 ++++++++++
 .../pom.xml                                        |  22 +++++++++++-
 .../src/main/java/demo/Application.java            |  31 ++++++++++++++++
 .../src/main/java/demo/HelloEndPoint.java          |  39 +++++++++++++++++++++
 .../classes/demo/Application.class                 | Bin 745 -> 0 bytes
 .../classes/demo/HelloEndPoint.class               | Bin 1243 -> 0 bytes
 .../classes-no-contract/demo/Application.class     | Bin 745 -> 0 bytes
 .../classes/demo/Application.class                 | Bin 745 -> 0 bytes
 .../classes/demo/HelloEndPoint.class               | Bin 1243 -> 0 bytes
 18 files changed, 242 insertions(+), 16 deletions(-)

diff --git a/pom.xml b/pom.xml
index bf8a1c7..6a92af8 100755
--- a/pom.xml
+++ b/pom.xml
@@ -260,6 +260,7 @@
           <plugin>
             <groupId>org.jacoco</groupId>
             <artifactId>jacoco-maven-plugin</artifactId>
+            <version>0.7.8</version>
           </plugin>
         </plugins>
       </build>
diff --git a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
index 641441b..866419c 100755
--- a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
+++ b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
@@ -88,7 +88,7 @@ public class GenerateContractsDocMojo extends AbstractMojo {
 
           ContractGenerator contractGenerator = new ContractGenerator(project);
           contractGenerator.generateAndOutput(contractLocation, "");
-          if (Objects.requireNonNull(tmpFileDir.listFiles()).length == 0) {
+          if (Objects.requireNonNull(tmpPath.toFile().listFiles()).length == 0) {
             LOGGER.info("no contract in the code");
             return;
           }
diff --git a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
index 1c301df..e37770f 100755
--- a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
+++ b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
@@ -43,12 +43,18 @@ import org.apache.maven.project.MavenProject;
 import org.junit.Rule;
 import org.junit.Test;
 
+import util.ClassMaker;
+
 
 public class GenerateContractsDocMojoTest {
 
   private static final String PLUGIN_GOAL = "generateDoc";
 
-  private static final String TEST_PROJECT = "project-generateContractsDoc";
+  private static final String TEST_PROJECT_WITHCONTRACT = "demo-with-contract";
+
+  private static final String TEST_PROJECT_NOCONTRACT = "demo-non-contract";
+
+  private static final String TEST_PROJECT_CONTRACTLOCATION = "contract";
 
   @Rule
   public MojoRule rule = new MojoRule();
@@ -59,25 +65,32 @@ public class GenerateContractsDocMojoTest {
   @Test
   public void testGenerateContractsDoc() throws Exception {
 
-    File baseDir = this.resources.getBasedir(TEST_PROJECT);
+    File baseDir = this.resources.getBasedir(TEST_PROJECT_WITHCONTRACT);
+    File baseDirNonContract = this.resources.getBasedir(TEST_PROJECT_NOCONTRACT);
+    File contractLocation = this.resources.getBasedir(TEST_PROJECT_CONTRACTLOCATION);
 
     File pom = new File(baseDir, "pom.xml");
     AbstractMojo generateContractsDocMojo = (AbstractMojo) this.rule.lookupMojo(PLUGIN_GOAL, pom);
     assertNotNull(generateContractsDocMojo);
 
-    String testProjectDir = baseDir + File.separator;
-    String testContractDir = testProjectDir + "contract";
-    String testDocumentDir = testProjectDir + "document";
+    String testDir = baseDir + File.separator;
+    String testDirNonContract = baseDirNonContract + File.separator;
+    String classesPath = "target/classes";
+    String testDocumentDir = testDir + "document";
+    String testContractDir = contractLocation.getCanonicalPath();
 
     if (!new File(testDocumentDir).exists()) {
-      assertTrue((new File(testDocumentDir)).mkdir());
+      assertTrue((new File(testDocumentDir)).mkdirs());
     }
 
     final MavenProject project = mock(MavenProject.class);
 
     // code has no contract
+      ClassMaker.compile(testDirNonContract);
+
     List<String> runtimeUrlPath = new ArrayList<>();
-    runtimeUrlPath.add(baseDir + "/classes-no-contract");
+    runtimeUrlPath.add(testDirNonContract + classesPath);
+
     given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
     rule.setVariableValueToObject(generateContractsDocMojo, "project", project);
 
@@ -93,8 +106,10 @@ public class GenerateContractsDocMojoTest {
     }
 
     // code has contract
+      ClassMaker.compile(testDir);
+
     runtimeUrlPath.remove(0);
-    runtimeUrlPath.add(baseDir + "/classes");
+    runtimeUrlPath.add(testDir + classesPath);
     given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
     rule.setVariableValueToObject(generateContractsDocMojo, "project", project);
 
@@ -121,9 +136,9 @@ public class GenerateContractsDocMojoTest {
       assertEquals("contract location is not exists", e.getMessage());
     }
 
-    String testEmptyDir = testProjectDir + "emptyDir";
+    String testEmptyDir = testDir + "emptyDir";
     if (!new File(testEmptyDir).exists()) {
-      assertTrue((new File(testEmptyDir)).mkdir());
+      assertTrue((new File(testEmptyDir)).mkdirs());
     }
     try {
       rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "contract");
diff --git a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java
index 97c8273..0475a45 100755
--- a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java
+++ b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsMojoTest.java
@@ -35,10 +35,14 @@ import org.apache.servicecomb.toolkit.plugin.GenerateContractsMojo;
 import org.junit.Rule;
 import org.junit.Test;
 
+import util.ClassMaker;
+
 public class GenerateContractsMojoTest {
 
   private static final String PLUGIN_GOAL = "generateContracts";
 
+  private static final String TEST_PROJECT_CONTRACT = "demo-with-contract";
+
   @Rule
   public MojoRule rule = new MojoRule();
 
@@ -47,7 +51,7 @@ public class GenerateContractsMojoTest {
 
   @Test
   public void testGenerateContracts() throws Exception {
-    executeMojo("project-generateContracts", PLUGIN_GOAL);
+    executeMojo(TEST_PROJECT_CONTRACT, PLUGIN_GOAL);
   }
 
   protected void executeMojo(String projectName, String goalName) throws Exception {
@@ -57,7 +61,8 @@ public class GenerateContractsMojoTest {
     assertTrue(baseDir.exists());
     assertTrue(baseDir.isDirectory());
 
-    File pom = new File(baseDir, "pom.xml");
+    String pomFile = "pom-gencontract.xml";
+    File pom = new File(baseDir, pomFile);
     AbstractMojo generateContractsMojo = (AbstractMojo) this.rule.lookupMojo(goalName, pom);
 
     assertNotNull(generateContractsMojo);
@@ -65,8 +70,11 @@ public class GenerateContractsMojoTest {
 
     final MavenProject project = mock(MavenProject.class);
     given(project.getFile()).willReturn(pom);
+
+    ClassMaker.compile(baseDir + File.separator + pomFile);
+
     List<String> runtimeUrlPath = new ArrayList<>();
-    runtimeUrlPath.add(baseDir + "/classes");
+    runtimeUrlPath.add(baseDir + File.separator + "target/classes");
     given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
 
     rule.setVariableValueToObject(generateContractsMojo, "project", project);
diff --git a/toolkit-maven-plugin/src/test/java/util/ClassMaker.java b/toolkit-maven-plugin/src/test/java/util/ClassMaker.java
new file mode 100755
index 0000000..7c3f73b
--- /dev/null
+++ b/toolkit-maven-plugin/src/test/java/util/ClassMaker.java
@@ -0,0 +1,33 @@
+/*
+ * 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 util;
+
+import java.io.IOException;
+
+public class ClassMaker {
+
+  public static void compile(String projectPath) throws IOException {
+    Runtime runtime = Runtime.getRuntime();
+    Process exec = runtime.exec("mvn clean package -f " + projectPath);
+    try {
+      exec.waitFor();
+    } catch (InterruptedException e) {
+      e.printStackTrace();
+    }
+  }
+}
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/contract/swagger.yaml b/toolkit-maven-plugin/src/test/projects/contract/swagger.yaml
similarity index 100%
rename from toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/contract/swagger.yaml
rename to toolkit-maven-plugin/src/test/projects/contract/swagger.yaml
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml b/toolkit-maven-plugin/src/test/projects/demo-non-contract/pom-gencontract.xml
similarity index 79%
copy from toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml
copy to toolkit-maven-plugin/src/test/projects/demo-non-contract/pom-gencontract.xml
index 3af32db..c6adc8e 100755
--- a/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-non-contract/pom-gencontract.xml
@@ -29,6 +29,20 @@
   <name>This is for Test ONLY</name>
   <packaging>jar</packaging>
 
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>1.5.12.RELEASE</version>
+    <relativePath/> <!-- lookup parent from repository -->
+  </parent>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter</artifactId>
+    </dependency>
+  </dependencies>
+
   <build>
     <plugins>
       <plugin>
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml b/toolkit-maven-plugin/src/test/projects/demo-non-contract/pom.xml
similarity index 78%
copy from toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
copy to toolkit-maven-plugin/src/test/projects/demo-non-contract/pom.xml
index a2d913e..025fe44 100755
--- a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-non-contract/pom.xml
@@ -22,12 +22,27 @@
   <prerequisites>
     <maven>3.0.3</maven>
   </prerequisites>
+
   <groupId>org.apache.servicecomb.toolkit</groupId>
   <artifactId>this-is-for-test-only</artifactId>
-  <version>1.0-SNAPSHOT</version>
+  <version>0.1.0-SNAPSHOT</version>
   <name>This is for Test ONLY</name>
   <packaging>jar</packaging>
 
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>1.5.12.RELEASE</version>
+    <relativePath/> <!-- lookup parent from repository -->
+  </parent>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter</artifactId>
+    </dependency>
+  </dependencies>
+
   <build>
     <plugins>
       <plugin>
diff --git a/toolkit-maven-plugin/src/test/projects/demo-non-contract/src/main/java/demo/Application.java b/toolkit-maven-plugin/src/test/projects/demo-non-contract/src/main/java/demo/Application.java
new file mode 100755
index 0000000..4a4a3c2
--- /dev/null
+++ b/toolkit-maven-plugin/src/test/projects/demo-non-contract/src/main/java/demo/Application.java
@@ -0,0 +1,31 @@
+/*
+ * 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 demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+  public Application() {
+  }
+
+  public static void main(String[] args) {
+    SpringApplication.run(Application.class, args);
+  }
+}
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom-gencontract.xml
similarity index 75%
rename from toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml
rename to toolkit-maven-plugin/src/test/projects/demo-with-contract/pom-gencontract.xml
index 3af32db..99bebfe 100755
--- a/toolkit-maven-plugin/src/test/projects/project-generateContracts/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom-gencontract.xml
@@ -29,6 +29,25 @@
   <name>This is for Test ONLY</name>
   <packaging>jar</packaging>
 
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>1.5.12.RELEASE</version>
+    <relativePath/> <!-- lookup parent from repository -->
+  </parent>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-web</artifactId>
+    </dependency>
+
+  </dependencies>
+
   <build>
     <plugins>
       <plugin>
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
similarity index 74%
rename from toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
rename to toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
index a2d913e..6c799da 100755
--- a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
@@ -22,12 +22,32 @@
   <prerequisites>
     <maven>3.0.3</maven>
   </prerequisites>
+
   <groupId>org.apache.servicecomb.toolkit</groupId>
   <artifactId>this-is-for-test-only</artifactId>
-  <version>1.0-SNAPSHOT</version>
+  <version>0.1.0-SNAPSHOT</version>
   <name>This is for Test ONLY</name>
   <packaging>jar</packaging>
 
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>1.5.12.RELEASE</version>
+    <relativePath/> <!-- lookup parent from repository -->
+  </parent>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-web</artifactId>
+    </dependency>
+
+  </dependencies>
+
   <build>
     <plugins>
       <plugin>
diff --git a/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/Application.java b/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/Application.java
new file mode 100755
index 0000000..4a4a3c2
--- /dev/null
+++ b/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/Application.java
@@ -0,0 +1,31 @@
+/*
+ * 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 demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+  public Application() {
+  }
+
+  public static void main(String[] args) {
+    SpringApplication.run(Application.class, args);
+  }
+}
diff --git a/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/HelloEndPoint.java b/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/HelloEndPoint.java
new file mode 100755
index 0000000..69e40c5
--- /dev/null
+++ b/toolkit-maven-plugin/src/test/projects/demo-with-contract/src/main/java/demo/HelloEndPoint.java
@@ -0,0 +1,39 @@
+/*
+ * 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 demo;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping({"/hello"})
+public class HelloEndPoint {
+  public HelloEndPoint() {
+  }
+
+  @GetMapping({"/sayHello"})
+  public String sayHello(String name) {
+    return "Hello, " + name;
+  }
+
+  @GetMapping({"/sayHi"})
+  public String sayHi(String name) {
+    return "Hi, " + name;
+  }
+}
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/Application.class b/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/Application.class
deleted file mode 100644
index df126b8..0000000
Binary files a/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/Application.class and /dev/null differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/HelloEndPoint.class b/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/HelloEndPoint.class
deleted file mode 100644
index c90473d..0000000
Binary files a/toolkit-maven-plugin/src/test/projects/project-generateContracts/classes/demo/HelloEndPoint.class and /dev/null differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class
deleted file mode 100644
index df126b8..0000000
Binary files a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class and /dev/null differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class
deleted file mode 100644
index df126b8..0000000
Binary files a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class and /dev/null differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class
deleted file mode 100644
index c90473d..0000000
Binary files a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class and /dev/null differ