Using Maven to install Bower components with Bower

2019-06-24 08:23发布

问题:

I have bower installed globally using NPM. In my Maven project I have a bower.json file, I am using the exec-maven-plugin to install the bower components on build, however it failed because it "cannot run the program 'bower' in directory" which makes sense because Bower is not locally installed in the project directory.

However I want to use the global version of Bower so I don't have separate Bower programs in all my projects, it works if I go to the directory in terminal and manual run 'bower install'.

Question: I want Maven to use my global Bower version to avoid me having duplicate copies of Bower in each project, how do I do this?

This is the code from my POM file that runs the plugin and execution:

 <plugin>

  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.3.2</version>

  <executions>

    <execution>
      <id>exec-bower-install</id>

      <phase>generate-sources</phase>
      <configuration>

        <executable>bower</executable>

        <arguments>
          <argument>install</argument>
        </arguments>

      </configuration>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>

  </executions>
</plugin>

回答1:

don't forget the workingDirectory and put your bower.json inside root folder

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>exec-maven-plugin</artifactId>
   <executions>
     <execution>
     <phase>generate-sources</phase>
     <goals>
      <goal>exec</goal>
     </goals>
     </execution>
   </executions>
   <configuration>
     <executable>bower</executable>
     <arguments>
      <argument>install</argument>
     </arguments>
     <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
   </configuration>
</plugin>


回答2:

After a lot of testing, I've figured out it was a problem with my IDE, for some reason Netbeans gives me an error when it tries to build the project during the plugin execution, however when I run the Maven project straight from command line, it builds!



回答3:

Configure the executable property to point to the full directory of the bower executable, for example:

 <executable>c:/bower_directory/bower</executable>