Maven在复制依赖期间不会被排除

时间:2015-09-24 22:20:45

标签: java maven

我有一个使用Netty 4.0.29的项目,我有另一个依赖于netty 3.9.0的依赖项。我进行了排除但是当我运行copy-dependencies时它仍然在3.9.0中使用。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Wall extends JApplet implements ActionListener {

//Component declaration
    JLabel directions;
    JTextField input = new JTextField(10);
    private JButton go;

    private WallPanel wallPanel;

//Method declaration
    public void init() {
        getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
        //Set JButton and JLabel
        setLayout(new BorderLayout());

        JPanel controls = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
        go = new JButton("Go!");
        go.setBackground(Color.GREEN);
        go.setFocusPainted(false);
        go.addActionListener(this);
        controls.add(directions, gbc);
        controls.add(input, gbc);
        controls.add(go, gbc);

        wallPanel = new WallPanel();

        add(controls, BorderLayout.NORTH);
        add(wallPanel);
    }

    public void actionPerformed(ActionEvent ae) {
        String text = input.getText();
        wallPanel.setRowCount(Integer.parseInt(text));
        repaint();
    }

    public class WallPanel extends JPanel {

        private int rowCount;

        public void setRowCount(int rowCount) {
            this.rowCount = rowCount;
            repaint();
        }

        public int getRowCount() {
            return rowCount;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            int startX = 50;
            int startY = 0;
            int width = 50;
            int height = 20;
            int spacing = 2;
            int xOffset = 0;
            for (int row = 0; row < getRowCount(); row++) {
                int y = startY + (row * (height + spacing));
                if (row % 2 == 0) {
                    xOffset = width / 2;
                } else {
                    xOffset = 0;
                }
                for (int col = 0; col < 8; col++) {
                    int x = xOffset + (startX + (col * (width + spacing)));
                    g.setColor(Color.RED);
                    g.fillRect(x, y, width, height);
                }
            }
        }

    }
}

如果我运行mvn依赖:树有这个排除,我发现它确实被排除在外:

    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.9.31</version>
        <exclusions>
            <exclusion>
                <groupId>io.netty</groupId>
                <artifactId>netty</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

但是当我运行mvn clean依赖:copy-dependencies时,我看到jar 3.9.0与4.0.29一起被复制。根据文档和Google,在排除时不应复制。

[INFO] +- com.ning:async-http-client:jar:1.9.31:compile

我尝试按照以下第一个答案的建议进行排除,但这不起作用。

[INFO] Copying netty-3.9.0.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-3.9.0.Final.jar
[INFO] Copying netty-all-4.0.29.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-all-4.0.29.Final.jar

我还添加了一个依赖项,如进一步建议:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>                               <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <excludeArtifactIds>io.netty:netty:3.9.0.Final</excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我做错了什么?

2 个答案:

答案 0 :(得分:2)

对于那些有同样问题的人。我使用mvn -X并发现依赖:tree省略了另外两个引用netty的jar。我为那些人添加了排除项目,我很乐意去。花了一整天的时间。

答案 1 :(得分:0)

如果您编写的不是库,则可以通过简单的方法来控制项目中任何依赖项的版本 - 根pom文件中的dependencyManagement块,例如:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
            <version>4.0.29.Final</version>
        </dependency>
    </dependencies>
</dependencyManagement>

此块中的其他奖励 - 您可以省略具体依赖项中的依赖项的版本和范围(具有相同的组ID,工件ID和包装)。

PS另外看看你的依赖关系让我问你:你确定这个依赖有单个maven工件id吗? netty-all-4.0.29.Final.jar - 似乎此工件应具有netty-all工件ID ...如果它们具有不同的工件ID,我的配方将无济于事。在这种情况下,您应该为maven-dependency-plugin定义构建配置,例如:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <excludeArtifactIds>io.netty:netty:3.9.0.Final</excludeArtifactIds>
            </configuration>
        </plugin>
    </plugins>
</build>

或在maven通话中使用-DexcludeArtifactIds参数