โค้ดยิม/หลักสูตรจาวา/โมดูล 3/ปลั๊กอิน Maven ที่มีประโยชน์

ปลั๊กอิน Maven ที่มีประโยชน์

ระดับ, บทเรียน
มีอยู่

ที่เก็บ maven ของคุณบน GitHub

นักพัฒนาสามารถอัปโหลดไลบรารีของตนไปยัง GitHub ซึ่งมีปลั๊กอินไซต์-maven-pluginพิเศษ ลองดูตัวอย่างการใช้งาน:

<project>
    <properties>
        <github.global.server>github</github.global.server>
        <github.maven-plugin>0.9</github.maven-plugin>
    </properties>
 
    <distributionManagement>
    	<repository>
            <id>internal.repo</id>
        	<name>Temporary Staging Repository</name>
            <url>file://${project.build.directory}/mvn-repo</url>
    	</repository>
    </distributionManagement>
 
    <build>
    	<plugins>
        	<plugin>
                <artifactId>maven-deploy-plugin</artifactId>
    	        <version>2.8.1</version>
            	<configuration>
                    <altDeploymentRepository>
                        internal.repo::default::file://${project.build.directory}/mvn-repo
                    </altDeploymentRepository>
            	</configuration>
        	</plugin>
        	<plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>${github.maven-plugin}</version>
            	<configuration>
                	<message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
                	<branch>refs/heads/mvn-repo</branch>
                    <includes>**/*</includes>
                	<repositoryName>SuperLibrary</repositoryName>
                	<repositoryOwner>codegymu-student</repositoryOwner>
            	</configuration>
            	<executions>
                	<execution>
                    	<goals>
                            <goal>site</goal>
                    	</goals>
                        <phase>deploy</phase>
                	</execution>
            	</executions>
        	</plugin>
    	</plugins>
    </build>
 
</project>

ลองดูสิ่งที่เขียนที่นี่

การสร้างที่เก็บชั่วคราวในเครื่องจะเน้นเป็นสีน้ำเงิน ในทางเทคนิคแล้วมันเป็นเพียงโฟลเดอร์ แต่เราต้องการให้ Maven ถือว่าเป็นที่เก็บแยกต่างหาก

เราเน้นด้วยสีแดงถึงการเปิดตัวปลั๊กอิน maven-deploy-pluginซึ่งเราระบุว่าควรวางไลบรารีที่คอมไพล์ไว้ในที่เก็บชั่วคราวนี้

และสุดท้าย ปลั๊กอิน site-maven-plugin ถูกเน้นด้วยสีเขียวซึ่งควรนำไฟล์ทั้งหมดจากที่เก็บและส่งไฟล์เหล่านั้นไปยัง GitHub จำเป็นต้องมีคำอธิบายบางอย่างที่นี่ พารามิเตอร์ทั้งหมดแบ่งออกเป็นสองกลุ่ม: สิ่งที่ต้องกรอกและตำแหน่งที่จะเติม

สิ่งที่เรากรอก:
  • outputDirectory - ไดเร็กทอรีที่จะรับไฟล์สำหรับการส่ง
  • รวม - ตั้งค่าหน้ากากของไฟล์ที่จะส่ง
เราจะอัปโหลดที่ไหน:
  • repositoryOwner - ชื่อเจ้าของที่เก็บบน GitHub
  • repositoryName - ชื่อที่เก็บ
  • สาขา - ตั้งค่าสาขาพื้นที่เก็บข้อมูลบน GitHub ที่จะกระทำ
  • ข้อความ - ข้อความที่จะเพิ่มเมื่อยืนยัน

คุณต้องระบุการเข้าสู่ระบบและรหัสผ่านสำหรับพื้นที่เก็บข้อมูลของคุณใน Maven setting.xml :

<settings>
  <servers>
    <server>
  	<id>github</id>
      <username>[username]</username>
      <password>[password]</password>
    </server>
  </servers>
</settings>

หากต้องการเชื่อมต่อ (ใช้) ไลบรารีจากที่เก็บ GitHub กับโปรเจ็กต์อื่น คุณต้องระบุที่เก็บนี้ในpom.xml ของคุณ :

<repositories>
    <repository>
        <id>[name-project]-mvn-repo</id>
        <url>https://raw.github.com/[username]/[name-project]/mvn-repo/</url>
    	<snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
    	</snapshots>
	</repository>
</repositories>

หลังจากนั้น Maven จะเข้าใจว่าจะหาห้องสมุดได้จากที่ใด

  • [name-project]คือชื่อของโครงการ ในกรณีของเรา SuperLibrary
  • [ชื่อผู้ใช้]คือล็อกอินบน GitHub ในตัวอย่างคือผู้ใช้ codegym

บรรจุแอสเซมบลีลงในอิมเมจ Docker

เราอยู่ในยุคใหม่ที่เมื่อโปรเจ็กต์อันเป็นผลมาจากแอสเซมบลีสามารถวางไว้ในที่เก็บ Maven หรืออาจอยู่ในที่เก็บข้อมูลนักเทียบท่า

เพื่อให้ Maven และ Docker เป็นเพื่อนกัน เราจำเป็นต้องมีปลั๊กอิน docker-maven-plugin ไม่มีอะไรซับซ้อน:

  <build>
    <plugins>
  	  <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
    	<version>0.4.10</version>
    	<configuration>
          <dockerDirectory>${project.basedir}</dockerDirectory>
      	  <imageName>codegym/${project.artifactId}</imageName>
    	</configuration>
    	<executions>
      	  <execution>
            <phase>package</phase>
        	<goals>
          	<goal>build</goal>
        	</goals>
      	  </execution>
    	</executions>
  	  </plugin>
    </plugins>
  </build>

ไฮไลท์ด้วยสีน้ำเงินคือจุดที่เราเพิ่ม Bulid เป้าหมายไปยังขั้นตอนแพ็คเกจของงานสร้าง สามารถเรียกได้ด้วย คำสั่ง mvn docker: build

แท็กdockerDirectoryระบุโฟลเดอร์ที่ Dockerfile ตั้งอยู่ และตั้ง ชื่อรูปภาพโดยใช้ แท็ก imageName

หากโปรเจ็กต์ถูกบรรจุในไฟล์ jar ไฟล์นักเทียบท่าจะมีลักษณะดังนี้:

FROM java:11
EXPOSE 8080
ADD /target/demo.jar demo.jar
ENTRYPOINT ["java","-jar","demo.jar"]

หากคุณกำลังบรรจุเว็บแอปพลิเคชัน คุณอาจต้องเพิ่ม Tomcat:

FROM tomcat8
ADD sample.war ${CATALINA_HOME}/webapps/ROOT.war
CMD ${CATALINA_HOME}/bin/catalina.sh run
ความคิดเห็น
  • เป็นที่นิยม
  • ใหม่
  • เก่า
คุณต้องลงชื่อเข้าใช้เพื่อแสดงความคิดเห็น
หน้านี้ยังไม่มีความคิดเห็นใด ๆ