Подтвердить что ты не робот

Использование задачи antcontrib <if> через maven-antrun-plugin

В моем проекте maven java используется maven-antrun-plugin для выполнения deploy.xml ant script, который развертывает мое приложение. В файле deploy.xml используется задача <if>, и это, по-видимому, вызывает проблему;

[INFO] Выполнение задач
  [taskdef] Не удалось загрузить определения из ресурса net/sf/antcontrib/antlib.xml. Его не удалось найти.

Deploy:
[ИНФОРМАЦИЯ] ----------------------------------------------- -------------------------
[ERROR] СТРОИТЬ ОШИБКУ
[ИНФОРМАЦИЯ] ----------------------------------------------- -------------------------
[INFO] Произошло событие ant BuildException: при выполнении этой строки произошла следующая ошибка:
 E:\My_Workspace\xxxxxx\xxxxxx\xxxxxxx\deploy.xml: 24: Проблема: не удалось создать задачу или тип, если
Причина. Имя undefined.
Действие: Проверьте орфографию.
Действие: убедитесь, что объявлены какие-либо пользовательские задачи/типы.
Действие: проверьте, что любой <presetdef> /<macrodef> были сделаны заявления.

Вот конфигурация плагина antrun из моего pom;

<plugin>
    <inherited>false</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>remote-deploy</id>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

                        <property name="compile_classpath" refid="maven.compile.classpath" />
                        <property name="runtime_classpath" refid="maven.runtime.classpath" />
                        <property name="plugin_classpath" refid="maven.plugin.classpath" />

                        <echo message="compile classpath: ${compile_classpath}"/>
                        <echo message="runtime classpath: ${runtime_classpath}"/>
                        <echo message="plugin classpath: ${plugin_classpath}"/>

                        <ant antfile="${basedir}/deploy.xml">
                            <target name="deploy" />
                        </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>
</plugin>

.. и вот соответствующий раздел из моего deploy.xml;

<target name="deploy" if="deploy">
    <if>    <!-- line 24 -->
        <and>

Почему я смотрю в своем maven-репо, я вижу ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar, и когда я заглядываю в банку, я вижу net/sf/antcontrib/antcontrib.properties, поэтому проблем нет.

Когда я проверяю значения maven.compile.classpath, maven.compile.classpath и maven.compile.classpath, я не вижу ссылки на antcontrib, может ли это быть проблемой? Почему они не появляются, когда antcontrib определяется как зависимость?

4b9b3361

Ответ 1

Хорошо, я решил это.

Перемещение зависимостей из тега <build><plugin> и включение их в другие зависимости проекта, похоже, сделали трюк.

Ответ 2

Я думаю, что не очень хорошая идея добавить ant для компиляции пути к классам, чтобы запустить плагин maven.

Я использую Maven 3.0.4, и он работал, указав пространство имен для тегов ant -contrib, например:

<configuration>
  <target>
    <echo message="The first five letters of the alphabet are:"/>
    <ac:for list="a,b,c,d,e" param="letter" xmlns:ac="antlib:net.sf.antcontrib">
      <sequential>
        <echo>Letter @{letter}</echo>
      </sequential>
    </ac:for>
  </target>
</configuration>

Мои зависимости maven-antrun-plugin:

<dependencies>
  <dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>1.0b3</version>
    <exclusions>
      <exclusion>
        <groupId>ant</groupId>
        <artifactId>ant</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-nodeps</artifactId>
    <version>1.8.1</version>
  </dependency>
</dependencies>

Ответ 3

Я обнаружил, что вам нужно включить зависимость ant -contrib внутри плагина, которая позволит тегу taskdef найти antcontrib.properties

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>ant-contrib</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>20020829</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>copy-and-rename-template-files</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target name = "copy-and-rename-template-files">
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                            <if>
                                <available file="src/main/resources/docker/templates" type="dir"/>
                                <then>
                                    <copy todir="${project.build.directory}/docker">
                                        <fileset dir="src/main/resources/docker/templates">
                                            <include name="**/*"/>
                                        </fileset>
                                    </copy>


                                    <move todir="${project.build.directory}/docker">
                                        <fileset dir="${project.build.directory}/docker">
                                            <include name="*"/>
                                        </fileset>
                                        <mapper>
                                            <regexpmapper from="(.*)project(.*)" to="\1${project.artifactId}\2"/>
                                        </mapper>
                                    </move>
                                </then>

                                <else>
                                    <echo>src/main/resources/docker/templates does not exist, skipping processing docker templates</echo>
                                </else>
                            </if>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Ответ 4

другое решение будет: сохранить путь ant -contrib-1.0b3.jar к пути, а затем определить его следующим образом

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

затем

<target name="doSomething">
    <if>
        <equals arg1="${someProp}" arg2="YES" />
        <then>
            <echo message="It is YES" />
        </then>
        <else>
            <echo message="It is not YES" />
        </else>
    </if>
</target>

Здесь я приведу полный пример кода, который вы можете скачать https://www.surasint.com/run-ant-with-if-from-maven/