# x2h (hpm cli plugin)

> HPM CLI 插件X2H，转换Maven/Gradle/NPM包为hpm包并发布到HPM仓库.


## 如何安装?（独立使用）

```sh
npm install -g @ohos/hpm-cli-x2h
```

## 如何使用x2h命令?

```sh
x2h  <类型> [选项]
```

## 选项

- -p 或 preview  ---只预览生成的包，不发布

## 类型

- gradle         ---从通过maven插件先根据gradle.build生成pom4hpm.xml，再转换为bundle.json
- maven          ---从pom.xml转换为bundle.json
- npm            ---从package.json转换为bundle.json

## 集成在HPM-CLI使用（hpm-cli 1.1.0以上版本）

### 从Gradle转换

在build.gradle编译脚本中添加如下脚本，执行publish2hpm任务（或者将publish2hpm发布后操作）

``` groovy
plugins {
    id 'maven'
}

task publish2hpm {
    pom {
        project {
            url "https://www.your.site"
            properties = [
                "hpm.bundle.name":'@your_organizaition/bunle_name',
                "hpm.bundle.tags":"foo,bar",
                "hpm.bundle.keywords":"foo,bar",
                "hpm.bundle.dirs":'{"lib":["build/outputs/*ar/release/*.*ar"]}'
            ]
        }
    }.writeTo("pom4hpm.xml")
    doLast{
        if (System.properties['os.name'].toString().startsWith('Windows'))
            ['cmd', '/c', 'hpm','x2h','gradle'].execute()
        else
            "hpm x2h gradle".execute()
    }
}
```

### 从Maven转换

在pom.xml文件中project下增加如下信息

``` xml
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>hpm-publish</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>deploy</phase>
                        <configuration>
                            <executable>hpm</executable>
                            <arguments>
                                <argument>x2h</argument>
                                <argument>maven</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <hpm.bundle.name>@your_organizaition/bunle_name</hpm.bundle.name>
        <hpm.bundle.tags>ui,image</hpm.bundle.tags>
        <hpm.bundle.keywords>foo,bar</hpm.bundle.keywords>
        <hpm.bundle.dirs>{"lib":["build/outputs/*ar/release/*.*ar"]}</hpm.bundle.dirs>
    </properties>
```

### 从npm转换

在package.json文件中增加如下信息

``` json
  "scripts":{
    "postpublish":"hpm x2h npm"
  },
  "hpm":{
    "bundle.name":"@your_organizaition/bundle"
  }
```

### 注意

| hpm 的组件命名规则 为 @organization/bundle_name格式,organization为hpm平台注册的组织名，bundle_name只能由小写字母、数字、_构成，不超过200，故不能直接使用Maven、NPM的包名。

