使用Maven运行Solr
Solr是一个开放源代码搜索服务器,它是使用Lucene Core的索引和搜索功能构建的,它可以用于使用几乎任何编程语言来实现可扩展的搜索引擎。
尽管Solr具有许多优点,但建立一个开发环境并不是其中之一。 该博客文章描述了如何使用Maven运行Solr,并确保每个开发人员都使用相同的配置,架构和Solr版本。
我们的Maven构建的要求如下:
- 我们的Maven构建的属性必须从外部属性文件中读取。 该规则的唯一例外是,依赖项的版本号在我们的POM文件中声明。
- 启动我们的Solr实例时,构建过程必须将Solr配置文件复制到正确的目录。
- 当开发人员在命令提示符下执行mvn clean命令时,构建过程必须清除配置文件。
- 必须能够通过使用Jetty Maven插件启动我们的Solr实例。
我们可以通过执行以下步骤来满足这些要求:
- 创建一个POM文件。
- 获取所需的依赖项。
- 获取Solr配置文件。
- 创建属性文件,其中包含在我们的Maven构建中使用的属性。
- 编辑solr.xml文件。
- 配置属性Maven插件。
- 配置复制Maven插件。
- 配置Jetty Maven插件。
下面将更详细地描述这些步骤。
创建POM文件
首先,我们必须为Web应用程序项目创建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>
获取所需的依赖关系
我们唯一需要的依赖是Solr 4.1.0(战争)。 换句话说,我们唯一要做的就是将以下依赖项声明添加到POM文件的依赖项部分:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr</artifactId>
<version>4.1.0</version>
<type>war</type>
</dependency>
获取Solr配置文件
通过执行以下步骤,我们可以获取Solr配置文件:
- 下载Solr 4.1.0的二进制发行版 。
- 将下载的软件包解压缩到所需目录。
- 进入解压缩的Solr二进制发行版的根目录。
- 将以下文件从目录example / solr / collection1 / conf复制到目录src / main / config : admin-extra.html,admin-extra-menu.menu-bottom.html,admin-extra.menu-top.hml ,currency.xml,elevate.xml,mapping-FoldToASCII.txt,mapping-ISOLatin1Accent.txt,protwords.xml,schema.xml,solrconfig.xml,spellings.txt,stopwords.txt,同义词.txt和update-script.js 。
- 将从目录example / solr / collection1 / conf / lang找到的特定于语言的配置文件复制到目录src / main / config / lang中 。
- 将从目录example / solr / collection1 / conf / velocity目录中找到的Velocity宏和其他文件复制到目录src / main / config / velocity中 。
- 将从目录example / solr / collection1 / conf / xslt中找到的XSL样式表复制到目录src / main / config / xslt中 。
- 将solr.xml文件从目录exaple / solr / collection1复制到目录src / main / resources 。
- 创建目录src / main / webapp / WEB-INF 。 此目录是必需的,以便可以启动Solr实例。
现在,我们已经成功获取了所需的文件,并准备进行下一个阶段。
创建属性文件
接下来是创建在Maven构建中使用的属性文件,并将所需的构建配置文件配置添加到我们的POM文件中。 让我们继续前进,找出实现方法。
首先,我们创建了在Maven构建中使用的属性文件。 我们可以按照以下步骤进行操作:
- 将目录profile / dev创建到我们的Maven项目的根目录。
- 在profiles / dev目录中创建一个属性文件config.properties 。
我们的属性文件具有以下三个属性:
- solr.detault.core.directory属性指定默认核心目录的值。 这是在我们的Solr实例的主目录下创建的目录。 该目录存储我们的Solr实例及其数据的配置。
- solr.default.core.name属性指定默认核心的名称。
- solr.solr.home属性指出我们的Solr安装目录的主目录。
config.properties文件的内容如下所示:
#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构建的构建配置文件,并使用过滤来替换替换资源中包含的变量。 我们可以按照以下步骤进行操作:
- 创建一个名为dev的配置文件,并确保它是我们构建的默认配置文件。
- 声明一个名为build.profile.id的属性,并将其值设置为'dev'。
- 创建一个过滤器,该过滤器读取特定于配置文件的配置文件,并将从我们的资源中找到的变量替换为实际的属性值。
通过将以下配置文件声明添加到我们的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默认核心的名称和实例目录,所以我们必须对solr.xml文件进行更改。 这些更改描述如下:
- 必须将solr.default.core.name属性的值设置为cores元素的defaultCoreNameAttribute属性的值。
- 必须将solr.default.core.name属性的值设置为核心元素的name属性的值。
- 必须将solr.default.core.directory属性的值设置为核心元素的instanceDir属性的值。
solr.xml文件的内容如下所示:
<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的插件 。 我们可以按照以下步骤配置此插件:
- 确保从配置文件特定的配置文件中读取属性。
- 创建一个执行,该执行在Maven默认生命周期的初始化阶段中运行Properties Maven插件的read-project-properties目标。
- 创建一个执行,该执行在Maven清理生命周期的预清理阶段中运行Properties Maven插件的读取项目属性目标。
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>
配置复制Maven插件
我们将使用Copy Maven插件有两个目的:
- 启动Solr实例时,我们会将Solr配置文件复制到正确的目录中。
- 在命令提示符处执行命令mvn clean时,我们将删除Solr配置文件。
我们可以通过将以下XML添加到POM文件的插件部分来开始使用:
<plugin>
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
<!-- Add executions here -->
</executions>
</plugin>
让我们继续前进,了解如何配置Copy Maven插件以复制和删除Solr配置文件。
复制Solr配置文件
我们可以按照以下步骤复制Solr配置文件:
- 创建一个执行,该执行在Maven默认生命周期的编译阶段中运行Copy Maven插件的复制目标。
- 将solr.xml文件复制到我们的Solr实例的主目录。 复制文件时,请确保将属性过滤应用于文件。
- 将从src / main / config目录找到的文件复制到solr.solr.home / solr.default.core.directory / conf目录。
- 将从src / main / config / lang目录中找到的特定于语言的配置文件复制到solr.solr.home / solr.detault.core.directory / conf / lang目录中。
- 将src / main / config / velocity目录中找到的Velocity宏和其他文件复制到solr.solr.home / solr.detault.core.directory / conf / velocity目录。
- 将从src / main / config / xslt目录中找到的XSL样式表复制到solr.solr.home / solr.detault.core.directory / conf / xslt目录中。
我们执行的配置如下所示:
<execution>
<id>copy-solr-config</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<resources>
<!--
Copy solr.xml to correct directory and applies properties
filtering 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配置文件:
- 创建一个执行,该执行在干净的生命周期阶段运行Copy Maven插件的复制目标。
- 如果找不到目录,请确保构建不会失败。
- 删除在我们的Maven项目的根目录中创建的overlays目录。
- 删除从我们的Solr实例的主目录中找到的solr.xml文件。
- 删除从solr.solr.home / solr.default.core.directory目录中找到的conf目录。
我们执行的配置如下所示:
<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插件以运行Solr实例:
- 配置Jetty侦听端口8983。
- 确保从配置文件特定的配置文件中读取系统属性。 该属性文件包含一个名为solr.solr.home的属性,该属性指定我们的Solr实例的主目录。
- 指定我们应用程序的上下文路径为/ solr 。
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>
运行Solr
现在,我们已经创建了一个Maven构建,该构建可用于在开发环境中运行Solr。 启动Solr实例有两种选择:
- 我们可以在命令提示符下执行mvn jetty:run命令。
- 我们可以在命令提示符下执行mvn jetty:run-war命令。
启动Solr之后,我们可以使用以下URL地址访问其管理界面: http:// localhost:8983 / solr 。
Github提供了示例应用程序 。 本示例使用自定义架构,因为我计划在Spring Data Solr教程中使用它。 原始示例架构可从etc目录中找到。
翻译自: https://www.javacodegeeks.com/2013/05/running-solr-with-maven.html