Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更新<;最新>;安装工件时在maven metadata local中标记?_Maven - Fatal编程技术网

如何更新<;最新>;安装工件时在maven metadata local中标记?

如何更新<;最新>;安装工件时在maven metadata local中标记?,maven,Maven,在我正在进行的项目中,我们正在并行开发各种多模块项目,其中一些项目依赖于其他项目。因此,我们在开发过程中使用版本范围(例如,[0.0.1,))作为我们的内部依赖项,这样我们就可以始终使用最新的快照版本。(我知道这不是最佳实践,但至少目前我们还停留在当前的项目结构上。)我们已经设置了构建概要文件,以便在执行发布时,所有版本范围都将替换为release,以根据最新发布的版本进行编译 我们必须使用范围,而不是LATEST,因为在本地安装工件时,maven-metadata-local.xml中的标记永

在我正在进行的项目中,我们正在并行开发各种多模块项目,其中一些项目依赖于其他项目。因此,我们在开发过程中使用版本范围(例如,
[0.0.1,)
)作为我们的内部依赖项,这样我们就可以始终使用最新的快照版本。(我知道这不是最佳实践,但至少目前我们还停留在当前的项目结构上。)我们已经设置了构建概要文件,以便在执行发布时,所有版本范围都将替换为
release
,以根据最新发布的版本进行编译

我们必须使用范围,而不是
LATEST
,因为在本地安装工件时,maven-metadata-local.xml中的
标记永远不会更新,因此指定
LATEST
将获得部署到工件服务器的最新版本。但是范围的问题是,构建过程似乎没有更新e下载工件所有版本的所有元数据文件,以便能够确定最新版本。随着项目的进行,我们正在积累越来越多的版本和工件,因此我们的构建需要越来越长的时间。指定
latest
可以避免这一点,但这意味着本地工件安装的更改通常会被忽略没有接


在本地安装工件时,是否有任何方法可以更新maven-metadata-local.xml文件中的
标记?

如果您使用的是快照,除了从不使用版本范围之外,您不需要其他版本范围(仅在极少数情况下).在版本范围内,您的构建是不可复制的,我认为在任何情况下都应该避免

但是你可以使用这样的东西:

<version>[1.2.3,)</version

这可以由像Jenkins这样的CI解决方案来处理。但我觉得您在做一些基本的事情,特别是当您需要使用版本范围时。如何将这些内部依赖项定义为一个reactor pom中的模块?这样您就可以根据编译的源代码(在目标/类中)进行编译了我也遇到了同样的问题,所以我编写了一个maven插件来处理它。这是一个非常极端的解决方法,但确实有效

创建maven插件的文档已打开。您可以从命令行原型创建插件项目,并将此mojo添加到您的项目中

/**
 * Inserts a "latest" block into the maven-metadata-local.xml in the user's local
 * repository using the currently configured version number.
 * 
 * @version Sep 23, 2013
 */
@Mojo( name = "latest-version", defaultPhase = LifecyclePhase.INSTALL )
public class InstallLatestVersionMojo extends AbstractMojo {

/**
 * Location of the .m2 directory
 */
@Parameter( defaultValue = "/${user.home}/.m2/repository", property = "outputDir", required = true )
private File repositoryLocation;

@Parameter( defaultValue = "${project.groupId}", property = "groupId", required = true )
private String groupId;

@Parameter( defaultValue = "${project.artifactId}", property = "artifactId", required = true )
private String artifactId;

/**
 * Version to use as the installed version
 */
@Parameter( defaultValue = "${project.version}", property = "version", required = true )
private String version;

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        // Fetch the xml file to edit from the user's repository for the project
        File installDirectory = getInstallDirectory(repositoryLocation, groupId, artifactId);
        File xmlFile = new File(installDirectory, "maven-metadata-local.xml");
        Document xml = getXmlDoc(xmlFile);

        if (xml != null) {
            // Fetch the <latest> node
            Node nodeLatest = getNode(xml, "/metadata/versioning/latest");
            if (nodeLatest == null) {
                // If <latest> does not yet exist, insert it into the <versioning> block before <versions>
                nodeLatest = xml.createElement("latest");
                Node versioningNode = getNode(xml, "/metadata/versioning");
                if (versioningNode != null) {
                    versioningNode.insertBefore(nodeLatest, getNode(xml, "metadata/versioning/versions"));
                }
            }
            // set the version on the <latest> node to the newly installed version
            nodeLatest.setTextContent(version);
            // save the xml
            save(xmlFile, xml);
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void save(File xmlFile, Document xml) throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Result output = new StreamResult(xmlFile);
    Source input = new DOMSource(xml);
    transformer.transform(input, output);
}

private Node getNode(Document source, String path) throws XPathExpressionException{
    Node ret = null;
    XPathExpression xPath = getPath(path);
    NodeList nodes = (NodeList) xPath.evaluate(source, XPathConstants.NODESET);
    if(nodes.getLength() > 0 ) {
        ret = nodes.item(0);
    }
    return ret;
}

private XPathExpression getPath(String path) throws XPathExpressionException{
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.compile(path);
}

private File getInstallDirectory(File repositoryLocation, String groupId, String artifactId) {
    String group = groupId.replace('.', '/');
    return new File(repositoryLocation, group + "/" + artifactId);
}

private Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(xmlFile);
}

}
/**
*将“最新”块插入到用户本地文件中的maven-metadata-local.xml中
*使用当前配置的版本号的存储库。
* 
*@版本2013年9月23日
*/
@Mojo(name=“最新版本”,defaultPhase=LifecyclePhase.INSTALL)
公共类InstalllatesVersionMojo扩展了AbstractMojo{
/**
*.m2目录的位置
*/
@参数(defaultValue=“/${user.home}/.m2/repository”,property=“outputDir”,required=true)
私有文件存储位置;
@参数(defaultValue=“${project.groupId}”,property=“groupId”,required=true)
私有字符串groupId;
@参数(defaultValue=“${project.artifactId}”,property=“artifactId”,required=true)
私有字符串工件;
/**
*要用作已安装版本的版本
*/
@参数(defaultValue=“${project.version}”,property=“version”,required=true)
私有字符串版本;
public void execute()抛出MojoExecutionException,MojoFailureException{
试一试{
//从项目的用户存储库中获取要编辑的xml文件
File installDirectory=getInstallDirectory(repositoryLocation、groupId、artifactId);
File xmlFile=新文件(installDirectory,“maven metadata local.xml”);
文档xml=getXmlDoc(xmlFile);
if(xml!=null){
//获取节点
Node nodeLatest=getNode(xml,“/metadata/versioning/latest”);
if(nodeLatest==null){
//如果还不存在,请在之前将其插入块中
nodeLatest=xml.createElement(“最新”);
Node versioningNode=getNode(xml,“/metadata/versioning”);
if(versioningNode!=null){
insertBefore(nodeLatest,getNode(xml,“元数据/版本控制/版本”);
}
}
//将节点上的版本设置为新安装的版本
nodeLatest.setTextContent(版本);
//保存xml
保存(xmlFile,xml);
}
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有void save(文件xmlFile,文档xml)引发TransformerFactoryConfigurationError,TransformerException{
Transformer Transformer=TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
结果输出=新的StreamResult(xmlFile);
源输入=新的DOMSource(xml);
变压器。变换(输入、输出);
}
私有节点getNode(文档源,字符串路径)抛出XPathExpressionException{
节点ret=null;
XPathExpression xPath=getPath(路径);
NodeList节点=(NodeList)xPath.evaluate(源代码,XPathConstants.NODESET);
if(nodes.getLength()>0){
ret=节点。项(0);
}
返回ret;
}
私有XPathExpression getPath(字符串路径)抛出XPathExpressionException{
XPath=XPathFactory.newInstance().newXPath();
返回xpath.compile(path);
}
私有文件getInstallDirectory(文件repositoryLocation、字符串groupId、字符串artifactId){
字符串组=groupId.replace('.','/');
返回新文件(repositoryLocation,group+“/”+artifactId);
}
私有文档getXmlDoc(文件xmlFile)抛出ParserConfiguration异常、SAXException、IOException{
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
文件B
/**
 * Inserts a "latest" block into the maven-metadata-local.xml in the user's local
 * repository using the currently configured version number.
 * 
 * @version Sep 23, 2013
 */
@Mojo( name = "latest-version", defaultPhase = LifecyclePhase.INSTALL )
public class InstallLatestVersionMojo extends AbstractMojo {

/**
 * Location of the .m2 directory
 */
@Parameter( defaultValue = "/${user.home}/.m2/repository", property = "outputDir", required = true )
private File repositoryLocation;

@Parameter( defaultValue = "${project.groupId}", property = "groupId", required = true )
private String groupId;

@Parameter( defaultValue = "${project.artifactId}", property = "artifactId", required = true )
private String artifactId;

/**
 * Version to use as the installed version
 */
@Parameter( defaultValue = "${project.version}", property = "version", required = true )
private String version;

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        // Fetch the xml file to edit from the user's repository for the project
        File installDirectory = getInstallDirectory(repositoryLocation, groupId, artifactId);
        File xmlFile = new File(installDirectory, "maven-metadata-local.xml");
        Document xml = getXmlDoc(xmlFile);

        if (xml != null) {
            // Fetch the <latest> node
            Node nodeLatest = getNode(xml, "/metadata/versioning/latest");
            if (nodeLatest == null) {
                // If <latest> does not yet exist, insert it into the <versioning> block before <versions>
                nodeLatest = xml.createElement("latest");
                Node versioningNode = getNode(xml, "/metadata/versioning");
                if (versioningNode != null) {
                    versioningNode.insertBefore(nodeLatest, getNode(xml, "metadata/versioning/versions"));
                }
            }
            // set the version on the <latest> node to the newly installed version
            nodeLatest.setTextContent(version);
            // save the xml
            save(xmlFile, xml);
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void save(File xmlFile, Document xml) throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Result output = new StreamResult(xmlFile);
    Source input = new DOMSource(xml);
    transformer.transform(input, output);
}

private Node getNode(Document source, String path) throws XPathExpressionException{
    Node ret = null;
    XPathExpression xPath = getPath(path);
    NodeList nodes = (NodeList) xPath.evaluate(source, XPathConstants.NODESET);
    if(nodes.getLength() > 0 ) {
        ret = nodes.item(0);
    }
    return ret;
}

private XPathExpression getPath(String path) throws XPathExpressionException{
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.compile(path);
}

private File getInstallDirectory(File repositoryLocation, String groupId, String artifactId) {
    String group = groupId.replace('.', '/');
    return new File(repositoryLocation, group + "/" + artifactId);
}

private Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(xmlFile);
}

}