CodeGym /Kursus Jawa /3. modul /Nggawe proyek karo Maven

Nggawe proyek karo Maven

3. modul
tingkat , wulangan
kasedhiya

Nggunakake maven-deploy-plugin

Lan topik liyane sing menarik yaiku panyebaran otomatis paket sing dipasang. Ayo kita nggawe perpustakaan dhewe nggunakake Maven. Kepiye cara kanthi otomatis push menyang repositori Maven lokal, perusahaan, utawa pusat?

Maven duwe plugin maven-deploy-plugin khusus kanggo iki . Tuladha:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
    	<version>2.5</version>
    	<configuration>
          <file>${project.build.directory}\${project.artifactId}-src.zip</file>
          <url>${project.distributionManagement.repository.url}</url>
          <repositoryId>${project.distributionManagement.repository.id}</repositoryId>
          <groupId>${project.groupId}</groupId>
          <artifactId>${project.artifactId}</artifactId>
          <version>${project.version}</version>
      	  <packaging>zip</packaging>
          <pomFile>pom.xml</pomFile>
    	</configuration>
  	</plugin>

Kanthi setelan kasebut, sampeyan bisa nyurung perpustakaan sing dibangun menyang gudang Maven minangka paket sing bener. Kita ora bakal nganalisa proses iki kanthi rinci, nanging kanthi ringkes nimbang apa sing kedadeyan ing kene:

Tag file nemtokake file sing bakal di-push menyang gudang Maven minangka perpustakaan anyar.

Tag url minangka path menyang repositori Maven (lokal / perusahaan / ...).

Tag repositoryId nemtokake pengenal repositori sing bakal ditindakake.

Tag groupId , artifactId , versi nemtokake identifikasi paket standar ing gudang Maven. Kanthi telung parameter kasebut, perpustakaan bisa diidentifikasi kanthi unik.

Tag kemasan digunakake kanggo mesthekake yen asil dikirim minangka file zip siji. Yen sampeyan ora nemtokake, banjur bakal ana siji file jar, sanajan sampeyan duwe sawetara file jar.

Tag pomFile opsional lan ngidini sampeyan ngirim pom.xml liyane menyang repositori sing ora ngemot data sing didhelikake utawa overhead.

Nyebarake aplikasi web menyang Tomcat nggunakake Maven

Server web sing paling populer kanggo aplikasi web Java yaiku Apache Tomcat . Lan mesthi, kanthi bantuan Maven, sampeyan bisa masang file perang langsung menyang server Tomcat lokal utawa malah adoh.

Kita bakal sinau carane nginstal lan ngatur Tomcat mengko, nanging saiki kita mung bakal ndemek topik panyebaran otomatis aplikasi web kita.

Langkah siji. Kita kudu menehi akses Maven menyang server Tomcat. Kanggo nindakake iki, bukak file conf/tomcat-users.xml ing direktori ing ngendi Apache Tomcat dibukak lan tambahake peran manager-gui lan manager-script :

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="admin" roles="manager-gui,manager-script" />
</tomcat-users>

Langkah loro. Allow akses Maven kanggo Tomcat. Kanggo nindakake iki, bukak file $MAVEN_HOME/conf/settings.xml lan tambahake server:

<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
  <servers>
	<server>
  	<id>TomcatServer</id>
  	<username>admin</username>
  	<password>admin</password>
	</server>
  </servers>
</settings>

Langkah telu. Kita nambahake plugin khusus kanggo panyebaran otomatis aplikasi kita menyang Apache Tomcat. Plugin kasebut diarani tomcat7-maven-plugin . Miturut cara, ora digawe dening pangembang Maven, nanging dening pangembang Tomcat, sing bisa ditebak saka jeneng kasebut.

	<build>
    	<plugins>
        	<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            	<configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>TomcatServer</server>
                	<path>/simpleProject</path>
            	</configuration>
        	</plugin>
    	</plugins>
	</build>

Ing bagean konfigurasi, nemtokake:

  • url minangka alamat ing ngendi Tomcat mlaku lan dalan menyang manajer / teks
  • server - id server saka file settings.xml
  • path - alamat ing ngendi aplikasi sing disebarake bakal kasedhiya

Perintah manajemen penyebaran:

mvn tomcat7: nyebarake Pasang aplikasi menyang Tomcat
mvn tomcat7: undeploy Mbusak aplikasi saka Tomcat
mvn tomcat7: redeploy Pasang maneh aplikasi kasebut

Pasang karo Cargo Plugin

Plugin liyane sing migunani lan serba guna kanggo nyebarake aplikasi web yaiku Cargo Plugin . Dheweke ngerti carane nggarap macem-macem jinis server web. Mangkene carane nyebarake ing Apache Tomcat:

<build>
    <plugins>
    	<plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
        	<version>1.9.10</version>
        	<configuration>
            	<container>
                	<containerId>tomcat8x</containerId>
                    <type>installed</type>
                	<home>Insert absolute path to tomcat 7 installation</home>
            	</container>
            	<configuration>
                    <type>existing</type>
                    <home>Insert absolute path to tomcat 7 installation</home>
            	</configuration>
        	</configuration>
   	    </plugin>
    </plugins>
</build>

Kanggo nginstal aplikasi web ing Tomcat lokal, sampeyan mung kudu mbukak printah:

mvn install
mvn cargo:deploy

Yen kita pengin nyebarke menyang server web sing adoh, mula kita kudu nyetel hak akses menyang server iki. Kanggo nindakake iki, sampeyan mung kudu ndhaptar ing pom.xml :

<configuration>
	<container>
        <containerId>tomcat8x</containerId>
    	<type>remote</type>
	</container>
	<configuration>
    	<type>runtime</type>
    	<properties>
            <cargo.remote.username>admin</cargo.remote.username>
            <cargo.remote.password>admin</cargo.remote.password>
        	<cargo.tomcat.manager.url>http://localhost:8080/manager/text</cargo.tomcat.manager.url>
    	</properties>
	</configuration>
</configuration>

Pasang nganggo IntelliJ IDEA

Intellij IDEA nindakake kabeh karya dhewe, sampeyan mung perlu Tomcat sing diinstal .

Langkah siji. Nggawe konfigurasi Tomcat lokal:

Langkah loro. Banjur pilih Tomcat lokal:

Langkah telu. Konfigurasi Tomcat:

Langkah papat. Tambah path menyang folder Tomcat.

Langkah kaping lima. Kita nambah proyek kita minangka artefak kanggo Tomcat.

Kanggo nindakake iki, pindhah menyang tab Deployment banjur klik tombol + ing sisih tengen .

Mekaten.

Miturut cara, yen sampeyan kudu masang menyang server remot, mung pilih Remote Tomcat ing langkah kapindho.

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION