solr 的maven_使用Maven运行Solr
solr 的maven
使用Maven運(yùn)行Solr
Solr是一個開源搜索服務(wù)器,它是使用Lucene Core的索引和搜索功能構(gòu)建的,它可以用于使用幾乎任何編程語言來實(shí)現(xiàn)可擴(kuò)展的搜索引擎。
盡管Solr具有許多優(yōu)點(diǎn),但建立一個開發(fā)環(huán)境并不是其中之一。 這篇博客文章描述了我們?nèi)绾问褂肕aven運(yùn)行Solr,并確保每個開發(fā)人員都使用相同的配置,架構(gòu)和Solr版本。
我們的Maven構(gòu)建的要求如下:
- 我們的Maven構(gòu)建的屬性必須從外部屬性文件中讀取。 該規(guī)則的唯一例外是,依賴項的版本號在我們的POM文件中聲明。
- 啟動我們的Solr實(shí)例時,構(gòu)建過程必須將Solr配置文件復(fù)制到正確的目錄。
- 當(dāng)開發(fā)人員在命令提示符下執(zhí)行mvn clean命令時,構(gòu)建過程必須清除配置文件。
- 必須能夠通過使用Jetty Maven插件啟動我們的Solr實(shí)例。
通過執(zhí)行以下步驟,我們可以滿足這些要求:
下面將更詳細(xì)地描述這些步驟。
創(chuàng)建POM文件
首先,我們必須為Web應(yīng)用程序項目創(chuàng)建POM文件。 我們的POM文件的框架如下所示:
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.petrikainulainen.maven</groupId><artifactId>running-solr-with-maven</artifactId><packaging>war</packaging><version>0.1</version><profiles><!-- Add profile configuration here --></profiles><dependencies><!-- Add dependencies here --></dependencies><build><finalName>solr</finalName><!-- Add filter configuration here --><!-- Add resources configuration here --><plugins><!-- Add plugin configuration here --></plugins></build> </project>獲取所需的依賴關(guān)系
我們唯一需要的依賴是Solr 4.1.0(戰(zhàn)爭)。 換句話說,我們唯一要做的就是將以下依賴項聲明添加到我們的POM文件的依賴項部分:
<dependency><groupId>org.apache.solr</groupId><artifactId>solr</artifactId><version>4.1.0</version><type>war</type> </dependency>獲取Solr配置文件
通過執(zhí)行以下步驟,我們可以獲取Solr配置文件:
現(xiàn)在,我們已經(jīng)成功獲取了所需的文件,并準(zhǔn)備進(jìn)行下一個階段。
創(chuàng)建屬性文件
接下來是創(chuàng)建在Maven構(gòu)建中使用的屬性文件,并將所需的構(gòu)建配置文件配置添加到我們的POM文件中。 讓我們繼續(xù)前進(jìn),找出實(shí)現(xiàn)方法。
首先,我們創(chuàng)建了在Maven構(gòu)建中使用的屬性文件。 我們可以按照以下步驟進(jìn)行操作:
我們的屬性文件具有以下三個屬性:
- solr.detault.core.directory屬性指定默認(rèn)核心目錄的值。 這是在我們的Solr實(shí)例的主目錄下創(chuàng)建的目錄。 該目錄存儲我們的Solr實(shí)例及其數(shù)據(jù)的配置。
- solr.default.core.name屬性指定默認(rèn)核心的名稱。
- solr.solr.home屬性表示我們的Solr安裝目錄的主目錄。
config.properties文件的內(nèi)容如下所示:
#SOLR PROPERTIES #Configures the directory used to store the data and configuration of the Solr default core solr.default.core.directory=todo #Configures the name of the Solr default core. solr.default.core.name=todo#SYSTEM PROPERTIES #Configures the home directory of Solr. Set the preferred directory path here. solr.solr.home=其次,我們必須配置Maven構(gòu)建的構(gòu)建配置文件,并使用過濾來替換替換資源中包含的變量。 我們可以按照以下步驟進(jìn)行操作:
通過將以下配置文件聲明添加到我們的POM文件中,我們可以完成步驟1和步驟2:
<profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><properties><build.profile.id>dev</build.profile.id></properties> </profile>我們可以通過將以下XML添加到POM文件的build部分來完成第三步:
<filters><filter>${project.basedir}/profiles/${build.profile.id}/config.properties</filter> </filters> <resources><resource><filtering>true</filtering><directory>src/main/resources</directory></resource> </resources>編輯solr.xml文件
因為我們使用特定于配置文件的配置文件來配置Solr默認(rèn)核心的名稱和實(shí)例目錄,所以我們必須對solr.xml文件進(jìn)行更改。 這些更改描述如下:
solr.xml文件的內(nèi)容如下所示:
<solr persistent="true"><cores adminPath="/admin/cores" defaultCoreName="${solr.default.core.name}" host="${host:}" hostPort="${jetty.port:}" hostContext="${hostContext:}" zkClientTimeout="${zkClientTimeout:15000}"><core name="${solr.default.core.name}" instanceDir="${solr.default.core.directory}" /></cores> </solr>配置屬性Maven插件
因為我們希望從外部屬性文件中讀取POM文件中使用的所有屬性值,所以我們必須使用一個名為Properties Maven plugin的插件 。 我們可以按照以下步驟配置此插件:
Properties Maven插件的配置如下所示:
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>properties-maven-plugin</artifactId><version>1.0-alpha-2</version><configuration><files><!-- Properties are read from profile specific property file --><file>${project.basedir}/profiles/${build.profile.id}/config.properties</file></files></configuration><executions><!-- Load properties for the default lifecycle --><execution><id>default-lifecycle-properties</id><phase>initialize</phase><goals><goal>read-project-properties</goal></goals></execution><!-- Load properties for the clean lifecycle --><execution><id>clean-lifecycle-properties</id><phase>pre-clean</phase><goals><goal>read-project-properties</goal></goals></execution></executions> </plugin>配置復(fù)制Maven插件
我們將使用Copy Maven插件有兩個目的:
我們可以通過將以下XML添加到POM文件的插件部分來開始使用:
<plugin><groupId>com.github.goldin</groupId><artifactId>copy-maven-plugin</artifactId><version>0.2.5</version><executions><!-- Add executions here --></executions> </plugin>讓我們繼續(xù)前進(jìn),了解如何配置Copy Maven插件以復(fù)制和刪除Solr配置文件。
復(fù)制Solr配置文件
我們可以按照以下步驟復(fù)制Solr配置文件:
我們執(zhí)行的配置如下所示:
<execution><id>copy-solr-config</id><phase>compile</phase><goals><goal>copy</goal></goals><configuration><resources><!--Copy solr.xml to correct directory and applies propertiesfiltering to it.--><resource><directory>${project.basedir}/src/main/resources</directory><filtering>true</filtering><targetPath>${solr.solr.home}</targetPath><includes><include>solr.xml</include></includes></resource><!-- Copy configuration files --><resource><directory>${project.basedir}/src/main/config</directory><targetPath>${solr.solr.home}/${solr.default.core.directory}/conf</targetPath><excludes><exclude>lang</exclude><exclude>velocity</exclude><exclude>xslt</exclude></excludes></resource><!-- Copy language specific configuration files --><resource><directory>${project.basedir}/src/main/config/lang</directory><targetPath>${solr.solr.home}/${solr.default.core.directory}/conf/lang</targetPath></resource><!-- Copy Velocity macros and other files --><resource><directory>${project.basedir}/src/main/config/velocity</directory><targetPath>${solr.solr.home}/${solr.default.core.directory}/conf/velocity</targetPath></resource><!-- Copy XSL style sheets --><resource><directory>${project.basedir}/src/main/config/xslt</directory><targetPath>${solr.solr.home}/${solr.default.core.directory}/conf/xslt</targetPath></resource></resources></configuration> </execution>刪除Solr配置文件
我們可以按照以下步驟刪除Solr配置文件:
我們執(zhí)行的配置如下所示:
<execution><id>clean-solr</id><phase>clean</phase><goals><goal>copy</goal></goals><configuration><failIfNotFound>false</failIfNotFound><resources><!-- Clean the overlays directory from the project root directory --><resource><clean>true</clean><cleanEmptyDirectories>true</cleanEmptyDirectories><directory>${project.basedir}/overlays</directory><includes><include>**/**</include></includes></resource><!-- Remove the solr.xml file --><resource><clean>true</clean><directory>${solr.solr.home}</directory><includes><include>solr.xml</include></includes></resource><!-- Remove the conf directory --><resource><clean>true</clean><cleanEmptyDirectories>true</cleanEmptyDirectories><directory>${solr.solr.home}/${solr.default.core.directory}</directory><includes><include>conf</include></includes></resource></resources></configuration> </execution>配置Jetty Maven插件
我們可以按照以下步驟配置Jetty Maven插件以運(yùn)行Solr實(shí)例:
Jetty Maven插件的配置如下所示:
<plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>8.1.8.v20121106</version><configuration><stopPort>9966</stopPort><stopKey>stop</stopKey><connectors><!-- Listen to port 8983 --><connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"><port>8983</port><maxIdleTime>60000</maxIdleTime></connector></connectors><!-- Read system properties from profile specific configuration file --><systemPropertiesFile>${project.basedir}/profiles/${build.profile.id}/config.properties</systemPropertiesFile><webApp><contextPath>/solr</contextPath></webApp></configuration> </plugin>運(yùn)行Solr
現(xiàn)在,我們已經(jīng)創(chuàng)建了一個Maven構(gòu)建,該構(gòu)建可用于在開發(fā)環(huán)境中運(yùn)行Solr。 啟動Solr實(shí)例有兩種選擇:
- 我們可以在命令提示符下執(zhí)行mvn jetty:run命令。
- 我們可以在命令提示符下執(zhí)行mvn jetty:run-war命令。
啟動Solr之后,我們可以使用以下URL地址訪問其管理界面: http:// localhost:8983 / solr 。
Github提供了示例應(yīng)用程序 。 本示例使用自定義架構(gòu),因為我計劃在Spring Data Solr教程中使用它。 原始示例架構(gòu)可從etc目錄中找到。
翻譯自: https://www.javacodegeeks.com/2013/05/running-solr-with-maven.html
solr 的maven
總結(jié)
以上是生活随笔為你收集整理的solr 的maven_使用Maven运行Solr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 随身wifi的特点是什么
- 下一篇: java嵌入式db_Java DB嵌入式